pywin32

ydq19892008 2010-09-26 09:34:35
我刚学Python时间不长,现在要做一个处理excel的东西,要把很多个excel表里的同一项数据读出来然后做运算,网上很多人说用pywin32,请问各位哪里有pywin32的教程啊,另外Python gui开发用什么比较快?急,谢谢各位了。
...全文
527 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
firewing_king 2011-06-08
  • 打赏
  • 举报
回复
还是牛人多哦,太多需要学的了。。。。。。纠结中
guzl86 2010-10-03
  • 打赏
  • 举报
回复
我前段时间刚刚做了这方面的工作,给你一段代码,希望对你有所帮助。
from win32com.client import Dispatch
import win32com.client

class easyExcel:
"""A utility to make it easier to get at Excel. Remembering
to save the data is your problem, as is error handling.
Operates on one workbook at a time."""

def __init__(self, filename=None):
self.xlApp = win32com.client.Dispatch('Excel.Application')
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''

def save(self, newfilename=None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()

def close(self):
self.xlBook.Close(SaveChanges=0)
del self.xlApp

def GetExcelLineCount(self):
count = 0
sht = self.xlBook.Worksheets('第一轮')
while True:
if sht.Cells(count + 1, 1).Value == None:
break
count += 1
return count
def getCell(self, sheet, row, col):
"Get value of one cell"
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row, col).Value

def setCell(self, sheet, row, col, value):
"set value of one cell"
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Value = value

def getRange(self, sheet, row1, col1, row2, col2):
"return a 2d array (i.e. tuple of tuples)"
sht = self.xlBook.Worksheets(sheet)
return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value

if __name__ == "__main__":
xls = easyExcel('D:/用户目录/pythonworkspace/excel2doc/src/test.xls')
lineNum = xls.GetExcelLineCount()
for i in range(1,lineNum + 1):
for j in range(1,8):
print i,j,xls.getCell('第一轮', i, j)
xls.save()
xls.close()
javacode007 2010-09-26
  • 打赏
  • 举报
回复

from win32com.client import Dispatch

filename = 'test.xls'
xlApp = Dispatch('Excel.Application')
xlBook = xlApp.Workbooks.Open(filename)
xlSht = xlBook.Worksheets('sheet1')

#打印第一行第一列的数据(其他操作类似)
print xlSht.Cells(1,1).Value

#赋值第一行第一列的数据(其他操作类似)
xlSht.Cells(1,1).Value = "Some Value"

#关闭文件
xlBook.Close(SaveChanges=0)
del xlApp


javacode007 2010-09-26
  • 打赏
  • 举报
回复
安装 pywin32 后,它会自带教程的啊(不过要操作 Excel,个人推荐您学习 Excel VBA 相关编程,接口都差不啦)
Python 的 GUI 开发,有很多都支持啊,不过差不都不太大,熟练了就快啦。

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧