Python win32com simple l Method for Exce operations (mandatory), win32comexce
Example:
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): # open a file or create a new file (if it does not exist) self. xlApp = win32com. client. dispatch ('excel. application ') if filename: self. filename = filename self. xlBook = se Lf. xlApp. workbooks. open (filename) else: self. xlBook = self. xlApp. workbooks. add () self. filename = ''def save (self, newfilename = None): # save the file if newfilename: self. filename = newfilename self. xlBook. saveAs (newfilename) else: self. xlBook. save () def close (self): # close the file self. xlBook. close (SaveChanges = 0) del self. xlApp def getCell (self, sheet, row, col): # Get the cell data "Get value of one cell" sht = self. xlBook. Worksheets (sheet) return sht. cells (row, col ). value def setCell (self, sheet, row, col, value): # set the cell data "set value of one cell" sht = self. xlBook. worksheets (sheet) sht. cells (row, col ). value = value def setCellformat (self, sheet, row, col): # set the cell data "set value of one cell" sht = self. xlBook. worksheets (sheet) sht. cells (row, col ). font. size = 15 # font Size. cells (row, col ). font. bold = True # whether to use the simhei sht. cell S (row, col ). name = "Arial" # font type sht. cells (row, col ). interior. colorIndex = 3 # Table background # sht. range ("A1 "). borders. lineStyle = xlDouble sht. cells (row, col ). borderAround () # Table border sht. rows (3 ). rowHeight = 30 # Row Height sht. cells (row, col ). horizontalAlignment =-4131 # horizontally centered xlCenter sht. cells (row, col ). verticalAlignment =-4160 # def deleteRow (self, sheet, row): sht = self. xlBook. worksheets (sheet) sht. rows (row ). delete () # Delete the row sht. columns (row ). delete () # Delete the def getRange (self, sheet, row1, col1, row2, col2) column: # obtain data in a region, returns a two-dimensional tuples "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 an image "Insert a picture in sheet" sht = self. xlBook. worksheets (sheet) sht. sh Apes. addPicture (pictureName, 1, 1, Left, Top, Width, Height) def cpSheet (self, before): # copy a worksheet "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) # below are some test code. 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 ()
The above simple method of operating Exce in Python win32com (mandatory) is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.