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()