python读写Excel文件

python要读写excel文件,需要另外在安装一个扩展pywin32,如果是使用的python2.5版本,对应的pywin32为pywin32-212.win32-py2.5.exe(下载地址:http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/p/py/pywin32/pywin32-212.win32-py2.5.exe)。

 import win32com

xlsfilename = r’D:\test.xls’    #test.xls 文件需要存在
inputstr = ‘input’
outputstr = ‘output’

xlsobj = win32com.client.Dispatch(‘Excel.Application’)
xlsfile = xlsobj.Workbooks.Open(xlsfilename)

xlssheet = xlsfile.Worksheets.Item(1)
xlssheet.Activate()

#在第1行,第1列写入imputstr;在第1行,第2列写入ouputstr
xlssheet.Cells(1, 1).FormulaR1C1=str(inputstr)
xlssheet.Cells(1, 2).FormulaR1C1=str(outputstr)

#读取第1行,第2列单元格的文本
putstr = xlssheet.Cells(1, 2).Text
print putstr

xlsfile.Save()
xlsfile.Close()

发表评论

邮箱地址不会被公开。 必填项已用*标注

机器人检查 *

分享我的最新文章标题到这里

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据