Python win32com 操作Exce的l簡(jiǎn)單方法(必看)
實(shí)例如下:
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): #打開(kāi)文件或者新建文件(如果不存在的話) 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): #關(guān)閉文件 self.xlBook.Close(SaveChanges=0) del self.xlApp def getCell(self, sheet, row, col): #獲取單元格的數(shù)據(jù) "Get value of one cell" sht = self.xlBook.Worksheets(sheet) return sht.Cells(row, col).Value def setCell(self, sheet, row, col, value): #設(shè)置單元格的數(shù)據(jù) "set value of one cell" sht = self.xlBook.Worksheets(sheet) sht.Cells(row, col).Value = value def setCellformat(self, sheet, row, col): #設(shè)置單元格的數(shù)據(jù) "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): #獲得一塊區(qū)域的數(shù)據(jù),返回為一個(gè)二維元組 "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): #復(fù)制工作表 "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) #下面是一些測(cè)試代碼。 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簡(jiǎn)單方法(必看)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn)
這篇文章主要為大家介紹了PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10python matplotlib庫(kù)繪制條形圖練習(xí)題
這篇文章主要介紹了python matplotlib庫(kù)繪制條形圖練習(xí)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點(diǎn)
Python函數(shù)的默認(rèn)值參數(shù)只會(huì)在函數(shù)定義處被解析一次,以后再使用時(shí)這個(gè)默認(rèn)值還是一樣,這在與可變參數(shù)共同使用時(shí)便會(huì)產(chǎn)生困惑,下面就來(lái)小議Python中自定義函數(shù)的可變參數(shù)的使用及注意點(diǎn)2016-06-06用pyqt5 給按鈕設(shè)置圖標(biāo)和css樣式的方法
今天小編就為大家分享一篇用pyqt5 給按鈕設(shè)置圖標(biāo)和css樣式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python入門學(xué)習(xí)之字符串與比較運(yùn)算符
這篇文章主要介紹了Python入門學(xué)習(xí)之字符串與比較運(yùn)算符,是Python語(yǔ)法中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10利用Python實(shí)現(xiàn)自定義連點(diǎn)器
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)自定義連點(diǎn)器,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08基于PyQt5制作一個(gè)數(shù)據(jù)圖表生成器
這篇文章主要介紹了如何利用PyQT5制作一個(gè)數(shù)據(jù)圖表生成器,可以通過(guò)Pyecharts模塊生成可視化的html數(shù)據(jù)圖表,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2022-02-02