實例如下:
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 setCellformat(self, sheet, row, col): #設置單元格的數據 "set value of one cell" sht = self.xlBook.Worksheets(sheet) sht.Cells(row, col).Font.Size = 15#字體大小 sht.Cells(row, col).Font.Bold = True#是否黑體 sht.Cells(row, col).Name = "Arial"#字體類型 sht.Cells(row, col).Interior.ColorIndex = 3#表格背景 #sht.Range("A1").Borders.LineStyle = xlDouble sht.Cells(row, col).BorderAround(1,4)#表格邊框 sht.Rows(3).RowHeight = 30#行高 sht.Cells(row, col).HorizontalAlignment = -4131 #水平居中xlCenter sht.Cells(row, col).VerticalAlignment = -4160 # def deleteRow(self, sheet, row): sht = self.xlBook.Worksheets(sheet) sht.Rows(row).Delete()#刪除行 sht.Columns(row).Delete()#刪除列 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)) def inserRow(self,sheet,row): sht = self.xlBook.Worksheets(sheet) sht.Rows(row).Insert(1) #下面是一些測試代碼。 if __name__ == "__main__": #PNFILE = r'c:/screenshot.bmp' xls = easyExcel(r'd:\jason.li\Desktop\empty_book.xlsx') #xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000) #xls.cpSheet('Sheet1') xls.setCell('sheet1',2,'A',88) row=1 col=1 print("*******beginsetCellformat********") # while(row<5): # while(col<5): # xls.setCellformat('sheet1',row,col) # col += 1 # print("row=%s,col=%s" %(row,col)) # row += 1 # col=1 # print("*******row********") # print("*******endsetCellformat********") # print("*******deleteRow********") # xls.deleteRow('sheet1',5) xls.inserRow('sheet1',7) xls.save() xls.close()
以上這篇Python win32com 操作Exce的l簡單方法(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
