日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python3.0 我的DailyReport 脚本(一) 使用COM操作Excel

發布時間:2025/7/25 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python3.0 我的DailyReport 脚本(一) 使用COM操作Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

其實不會用Python,跟風裝了Python3.0,看了幾天自帶的Manual,寫個日報的腳本玩玩,不用不要緊,一用感覺還挺好用的

先裝了xlwt,xlrd,pyexcelerator-0.6.4.1都不能使,原來那些都是Python2.X的產物,Python3.0居然不向下兼容,牛X,只好用pywin32-214.win32-py3.1.exe這玩意,據說就可以用COM ,有了VBA手冊,即可.帖代謝產物

Code
#!/usr/bin/env?python
#
coding=utf-8
#
author:haozes
#
COM讀寫Excel,輸出某月的日報報表
from?win32com.client?import?Dispatch??
import?win32com.client??
import?win32api
import?os
class?ExcelHelper:
????
def?__init__(self,?filename=None):
????????self.xlApp?
=?win32com.client.Dispatch('Excel.Application')??
????????
if?filename:
????????????self.filename
=filename
????????????
if?os.path.exists(self.filename):
????????????????self.xlBook
=self.xlApp.Workbooks.Open(filename)
????????????
else:
????????????????self.xlBook
=?self.xlApp.Workbooks.Add()
????????
else:
????????????self.xlBook
=?self.xlApp.Workbooks.Add()
????????????self.filename
='Untitle'
????
????
def?save(self,?newfilename=None):??
????????
if?newfilename:?????
????????????self.filename?
=?newfilename??
????????self.xlBook.SaveAs(self.filename)????
????????
????
def?close(self):??
????????self.xlBook.Close(SaveChanges
=0)??
????????
del?self.xlApp??
????
????
def?copySheet(self,?before):??
????????
"copy?sheet"??
????????shts?
=?self.xlBook.Worksheets??
????????shts(
1).Copy(None,shts(1))
????
????
def?newSheet(self,newSheetName):
????????sheet
=self.xlBook.Worksheets.Add()
????????sheet.Name
=newSheetName
????????sheet.Activate()
????
????
def?activateSheet(self,sheetName):
????????self.xlBook.Worksheets(sheetName).Activate()
????????
????
def?activeSheet(self):
????????
return?self.xlApp.ActiveSheet;????
????
????
def?getCell(self,?row,?col,sheet=None):??
????????
"Get?value?of?one?cell"??
????????
if?sheet:
????????????sht?
=?self.xlBook.Worksheets(sheet)??
????????
else:
????????????sht
=self.xlApp.ActiveSheet????
????????
return?sht.Cells(row,?col).Value??
????
????
def?setCell(self,?row,?col,?value,sheet=None):??
????????
"set?value?of?one?cell"??
????????
if?sheet:
?????????????sht?
=?self.xlBook.Worksheets(sheet)??
????????
else:
?????????????sht
=self.xlApp.ActiveSheet????
?????????
????????sht.Cells(row,?col).Value?
=?value??
????????
????
def?getRange(self,?row1,?col1,?row2,?col2,sheet=None):??
????????
"return?a?2d?array?(i.e.?tuple?of?tuples)"??
????????
if?sheet:
???????????sht?
=?self.xlBook.Worksheets(sheet)??
????????
else:
???????????sht
=self.xlApp.ActiveSheet????
????????
return?sht.Range(sht.Cells(row1,?col1),?sht.Cells(row2,?col2)).Value?
?????
????
def?mergeCell(self,?row1,?col1,?row2,?col2,sheet=None):?
????????
if?sheet:
????????????sht?
=?self.xlBook.Worksheets(sheet)??
????????
else:
????????????sht
=self.xlApp.ActiveSheet???
????????
return?sht.Range(sht.Cells(row1,?col1),?sht.Cells(row2,?col2)).Merge()?
????
def?rowsCount(self):
????????
"return?used?rows?count"
????????sht
=self.activeSheet()
????????
return??sht.UsedRange.Rows.Count
????????
if?__name__?==?"__main__":??
????
pass

????
????????????????
????????????

?

附送VBA手冊:http://files.cnblogs.com/solo/Vbaexcelxl10.zip

轉載于:https://www.cnblogs.com/solo/archive/2009/09/10/1564148.html

總結

以上是生活随笔為你收集整理的Python3.0 我的DailyReport 脚本(一) 使用COM操作Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。