This article mainly introduces the Excel file reading and writing class implemented by Python, which involves some common operations such as reading, writing, and printing in Excel, and has some reference value, for more information about how to read and write Excel files in Python, see the following example. Share it with you for your reference. The details are as follows:
# Coding = UTF-8 #################################### #################### filename: excelRW. py # author: defias # date: 2015-4-27 # function: read or write excel file ################################### ################### import xlrdimport xlwtimport xlutils. copy import OS. pathclass XlsEngine (): "The XlsEngine is a class for excel operation Usage: xlseng = XlsEngine ('filepath')" def _ init _ (self, xlsname) :" "" Define class variable "self.xls _ name = xlsname # file name self. xlrd_object = None # workbook object self. isopentrue = False # file open flag def open (self): "open a xls file Usage: xlseng. open () "" try: self. xlrd_object = xlrd.open_workbook(self.xls _ name) self. isopentrue = True print ('[% s, % s]. '% (self. isopentrue, self. xlrd_object) before T: self. isopentrue = False self. xlrd_object = None prin T ('open % s failed.'{self.xls _ name) def info (self): "" show xls file information Usage: xlseng.info () "if self. isopentrue = True: for sheetname in self. xlrd_object.sheet_names (): worksheet = self. xlrd_object.sheet_by_name (sheetname) print ('% s :( % d row, % d col ). '% (sheetname, worksheet. nrows, worksheet. ncols) else: print ('file % s is not open.'{self.xls _ name) def readcell (self, sheetname = 'sheet1', row N = 0, coln = 0): "" read file's a cell content Usage: xlseng. readcell ('sheetname', rown, coln) "try: if self. isopentrue = True: worksheets = self. xlrd_object.sheet_names () if sheetname not in worksheets: print ('% s is not exit. '% sheetname) return False worksheet = self. xlrd_object.sheet_by_name (sheetname) cell = worksheet. cell_value (rown, coln) print ('[file: % s, sheet: % s, row: % s, col: % s]: Fig _ n Ame, sheetname, rown, coln, cell) else: print ('file % s is not open.'{self.xls _ name) failed t: print ('readcell is false! Please check sheetn rown and coln is right. ') def readrow (self, sheetname = 'sheet1', rown = 0): "" read file's a row content Usage: xlseng. readrow ('sheetname', rown) "try: if self. isopentrue = True: worksheets = self. xlrd_object.sheet_names () if sheetname not in worksheets: print ('% s is not exit. '% sheetname) return False worksheet = self. xlrd_object.sheet_by_name (sheetname) row = worksheet. row_valu Es (rown) print ('[file: % s, sheet: % s, row: % s]: gradient s.'entries (self.xls _ name, sheetname, rown, row) else: print ('file % s is not open.'{self.xls _ name) failed t: print ('readrow is false! Please check sheetn rown is right. ') def readcol (self, sheetname = 'sheet1', coln = 0): "" read file's a col content Usage: xlseng. readcol ('sheetname', coln) "try: if self. isopentrue = True: worksheets = self. xlrd_object.sheet_names () if sheetname not in worksheets: print ('% s is not exit. '% sheetname) return False worksheet = self. xlrd_object.sheet_by_name (sheetname) col = worksheet. col_values (coln) Print ('[file: % s, sheet: % s, col: % s]: pais.'{(self.xls _ name, sheetname, coln, col) else: print ('file % s is not open.'{self.xls _ name) failed t: print ('readcol is false! Please check sheetn coln is right. ') def writecell (self, value = '', sheetn = 0, rown = 0, coln = 0):" "write a cell to file, other cell is not change Usage: xlseng. writecell ('str', sheetn, rown, coln) "try: if self. isopentrue = True: xlrd_objectc = xlutils. copy. copy (self. xlrd_object) worksheet = xlrd_objectc.get_sheet (sheetn) worksheet. write (rown, coln, value) xlrd_objectc.save(self.xls _ name) print ('writecel L value: % s to [sheet: % s, row: % s, col: % s] is ture. '% (value, sheetn, rown, coln) else: print ('file % s is not open.'{self.xls _ name) failed t: print ('writecell is false! Please check. ') def writerow (self, values = '', sheetn = 0, rown = 0, coln = 0):" "write a row to file, other row and cell is not change Usage: xlseng. writerow ('str1, str2, str3... strn', sheetn, rown. coln) "" try: if self. isopentrue = True: xlrd_objectc = xlutils. copy. copy (self. xlrd_object) worksheet = xlrd_objectc.get_sheet (sheetn) values = values. split (',') for value in values: worksheet. write (rown, coln, valu E) coln + = 1 xlrd_objectc.save(self.xls _ name) print ('writerow values: % s to [sheet: % s, row: % s, col: % s] is true. '% (values, sheetn, rown, coln) else: print ('file % s is not open.'{self.xls _ name) failed t: print ('writerow is false! Please check. ') def writecol (self, values = '', sheetn = 0, rown = 0, coln = 0):" "write a col to file, other col and cell is not change Usage: xlseng. writecol ('str1, str2, str3... ', sheetn, rown. coln) "" try: if self. isopentrue = True: xlrd_objectc = xlutils. copy. copy (self. xlrd_object) worksheet = xlrd_objectc.get_sheet (sheetn) values = values. split (',') for value in values: worksheet. write (rown, coln, value) r Own + = 1 xlrd_objectc.save(self.xls _ name) print ('writecol values: % s to [sheet: % s, row: % s, col: % s] is ture. '% (values, sheetn, rown, coln) else: print ('file % s is not open.'{self.xls _ name) failed t: print ('writecol is false! Please check. ') def filecreate (self, sheetnames = 'sheet1'): "" create a empty xlsfile Usage: filecreate ('sheetname1, sheetname2... ') "try: if OS .path.isfile(self.xls _ name): print (' % s is exit.'{self.xls _ name) return False workbook = xlwt. workbook () sheetnames = sheetnames. split (',') for sheetname in sheetnames: workbook. add_sheet (sheetname, cell_overwrite_ OK = True) workbook.save(self.xls _ name) print ('% S is created.'{self.xls _ name) character T: print ('filerator is false! Please check. ') def addsheet (self, sheetnames = 'sheet1'): "add sheets to a exit xlsfile Usage: addsheet ('sheetname1, sheetname2... ') "" try: if self. isopentrue = True: worksheets = self. xlrd_object.sheet_names () xlrd_objectc = xlutils. copy. copy (self. xlrd_object) sheetnames = sheetnames. split (',') for sheetname in sheetnames: if sheetname in worksheets: print ('% s is exit. '% sheetname) return False For sheetname in sheetnames: xlrd_objectc.add_sheet (sheetname, cell_overwrite_ OK = True) xlrd_objectc.save(self.xls _ name) print ('addsheet is ture. ') else: print ("file % s is not open \ n" %self.xls _ name) failed t: print ('addsheet is false! Please check. ') "def chgsheet (self, sheetn, values): def clear (self):" if _ name _ =' _ main __': # Initialize the object xlseng = XlsEngine ('E: \ Code \ Python \ test2.xls ') # create a file and specify the name of the sheet to be created, the default value is new sheet1 # print ("\ nxlseng. filecreate (): ") # xlseng. filecreate ('newesheet1, newesheet2, newesheet3') # open the file print ("xlseng. open (): ") xlseng. open () # Add sheet page print ("\ nxlseng. addsheet (): ") xlseng. addsheet ('addsheet1, addsheet2, addsheet3') # print the output file information ("\ nxlseng.info ():") xlseng.info () # read the data of cells in 3rd rows and 3rd columns on sheet 1 page (the data of cells in 1st rows and 1st columns on sheet 1 page is read by default) print ("\ nxlseng. readcell (): ") xlseng. readcell ('sheet1', 2nd) # read the data of the first row of the sheet1 page (the data of the second row of the sheet1 page is read by default) print ("\ nxlseng. readrow (): ") xlseng. readrow ('sheet1', 1) # read data in the 3rd columns on the sheet1 page (the data in the 1st columns on the sheet1 page is read by default) print ("\ nxlseng. readcol (): ") xlseng. readcol ('sheet1', 2) # write the string data 'I am writecell writed' to the 2nd rows and 4th columns of the first sheet page (empty strings are written to the 1st columns and 1st columns of the first sheet page by default) print ("\ nxlseng. writecell (): ") xlseng. writecell ('I am writecell writed', 3) # write a row of data to the first sheet. the values of each column are 'rowstr1, rowstr2, rowstr3 ', write data from 3rd rows and 4th columns (a row of data is written to the first sheet page by default, with a value of ''and written from 1st rows and 1st columns) print (" \ nxlseng. writerow (): ") xlseng. writerow ('rowstr1, rowstr2, rowstr3', 3) # write a column of data to the first sheet. the values of each row are 'colstr1, colstr2, colstr3, colstr4 ', write data from 4th columns in row 4th (a column of data is written to the first sheet page by default, with a value of ''and written from 1st columns in row 1st) print (" \ nxlseng. writecol (): ") xlseng. writecol ('colstr1, colstr2, colstr3, colstr4', 0, 3)
I hope this article will help you with Python programming.