Python利用win32com操作Excel .
2011-11-02 22:27
555 查看
使用COM接口,直接操作EXCEL(只能在Win上)
优点:可以满足绝大数要求。缺点:有些麻烦。:-)
这方面的例子很多,GOOGLE 看吧:-). 文档也可以参看OFFICE自带的VBA EXCEL 帮助文件(VBAXL.CHM)。这里面讲述了EXCEL VBA的编程概念,
不错的教程!另外,《Python Programming on Win32》书中也有很详细的介绍。这本书中给出了一个类来操作EXCEL 文件,可以很容易的加以扩展
view plaincopy to clipboardprint?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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 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
def addPicture(self, sheet, pictureName, Left, Top, Width, Height):
"Insert a picture in sheet"
sht = self.xlBook.Worksheets(sheet)
sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)
def cpSheet(self, before):
"copy sheet"
shts = self.xlBook.Worksheets
shts(1).Copy(None,shts(1))
"下面是一些测试代码。
if __name__ == "__main__":
PNFILE = r'c:/screenshot.bmp'
xls = easyExcel(r'D:/test.xls')
xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000)
xls.cpSheet('Sheet1')
xls.save()
xls.close()
优点:可以满足绝大数要求。缺点:有些麻烦。:-)
这方面的例子很多,GOOGLE 看吧:-). 文档也可以参看OFFICE自带的VBA EXCEL 帮助文件(VBAXL.CHM)。这里面讲述了EXCEL VBA的编程概念,
不错的教程!另外,《Python Programming on Win32》书中也有很详细的介绍。这本书中给出了一个类来操作EXCEL 文件,可以很容易的加以扩展
view plaincopy to clipboardprint?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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 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
def addPicture(self, sheet, pictureName, Left, Top, Width, Height):
"Insert a picture in sheet"
sht = self.xlBook.Worksheets(sheet)
sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)
def cpSheet(self, before):
"copy sheet"
shts = self.xlBook.Worksheets
shts(1).Copy(None,shts(1))
"下面是一些测试代码。
if __name__ == "__main__":
PNFILE = r'c:/screenshot.bmp'
xls = easyExcel(r'D:/test.xls')
xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000)
xls.cpSheet('Sheet1')
xls.save()
xls.close()
相关文章推荐
- python 利用win32com操作excel
- Python利用win32com操作Excel
- python 利用win32com操作excel
- Python操作Excel——win32com模块和xlrd+xlwt+xlutils组合
- 如何利用Python和win32编程避免重复性体力劳动(四)——下拉列表操作:CB_SETCURSEL、CBN_SELENDOK和CBN_SELCHANGE
- 如何利用Python和win32编程避免重复性体力劳动(二)——菜单操作:GetMenu,GetSubMenu,GetMenuItemID以及wParam的HIWORD&LOWORD
- 使用Python通过win32 COM打开Excel并添加Sheet的方法
- 使用Python通过win32 COM实现Excel的写入与保存
- 利用python操作excel
- Python3.0 我的DailyReport 脚本(一) 使用COM操作Excel
- Perl 利用Win32::OLE操作Excel 技巧和诀窍
- Python操作word常见方法示例【win32com与docx模块】
- python 利用 xdrlib 操作追加excel一种思路
- 如何利用Python和win32编程避免重复性体力劳动(四)——下拉列表操作:CB_SETCURSEL、CBN_SELENDOK和CBN_SELCHANGE
- win32下利用python操作printer
- python对excel操作 com_error错误
- 使用Python通过win32 COM打开Excel并添加Sheet
- 如何利用Python和win32编程避免重复性体力劳动(二)——菜单操作:GetMenu,GetSubMenu,GetMenuItemID以及wParam的HIWORD&LOWORD
- 如何利用Python和win32编程避免重复性体力劳动(三)——文本框操作:WM_GETTEXT 和WM_SETTEXT
- Python利用系统命令获取文件(夹)信息以及Python对Excel的简单操作