Python3.0 我的DailyReport 脚本(一) 使用COM操作Excel
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Asp.NET 中 Ajax 的配置使用
- 下一篇: XPath学习:轴(13)——names