Python win32com 操作Exce的l简单方法(必看)
实例如下:
fromwin32com.clientimportDispatch importwin32com.client classeasyExcel: """AutilitytomakeiteasiertogetatExcel.Remembering tosavethedataisyourproblem,asiserrorhandling. Operatesononeworkbookatatime.""" def__init__(self,filename=None):#打开文件或者新建文件(如果不存在的话) self.xlApp=win32com.client.Dispatch('Excel.Application') iffilename: self.filename=filename self.xlBook=self.xlApp.Workbooks.Open(filename) else: self.xlBook=self.xlApp.Workbooks.Add() self.filename='' defsave(self,newfilename=None):#保存文件 ifnewfilename: self.filename=newfilename self.xlBook.SaveAs(newfilename) else: self.xlBook.Save() defclose(self):#关闭文件 self.xlBook.Close(SaveChanges=0) delself.xlApp defgetCell(self,sheet,row,col):#获取单元格的数据 "Getvalueofonecell" sht=self.xlBook.Worksheets(sheet) returnsht.Cells(row,col).Value defsetCell(self,sheet,row,col,value):#设置单元格的数据 "setvalueofonecell" sht=self.xlBook.Worksheets(sheet) sht.Cells(row,col).Value=value defsetCellformat(self,sheet,row,col):#设置单元格的数据 "setvalueofonecell" 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# defdeleteRow(self,sheet,row): sht=self.xlBook.Worksheets(sheet) sht.Rows(row).Delete()#删除行 sht.Columns(row).Delete()#删除列 defgetRange(self,sheet,row1,col1,row2,col2):#获得一块区域的数据,返回为一个二维元组 "returna2darray(i.e.tupleoftuples)" sht=self.xlBook.Worksheets(sheet) returnsht.Range(sht.Cells(row1,col1),sht.Cells(row2,col2)).Value defaddPicture(self,sheet,pictureName,Left,Top,Width,Height):#插入图片 "Insertapictureinsheet" sht=self.xlBook.Worksheets(sheet) sht.Shapes.AddPicture(pictureName,1,1,Left,Top,Width,Height) defcpSheet(self,before):#复制工作表 "copysheet" shts=self.xlBook.Worksheets shts(1).Copy(None,shts(1)) definserRow(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()
以上这篇Pythonwin32com操作Exce的l简单方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。