1 # coding:utf-8 2 from openpyxl import load_workbook 3 4 #The first method of writing an existing xlsx file 5 # class Write_excel(object): 6 # '''Modify excel data''' 7 # def __init__( self, filename): 8 # self.filename = filename 9 # self.wb = load_workbook(self.filename) 10 # self.ws = self.wb.active # activate sheet 11 # 12 # def write(self, row_n, col_n , value): 13 # '''Write data, such as (2,3,"hello"), the second row and third column write data "hello"''' 14 # self.ws.cell(row_n, col_n,value ) 15 # self .wb.save(self.filename) 16 # 17 # we = Write_excel("mylogintest.xlsx") 18 # we.write(2,2,'pass3') 19 20 #Write an existing xlsx file The second Method 21 wb = load_workbook( " mylogintest.xlsx " ) #Generate an existing wookbook object 22 wb1 = wb.active #Activate sheet 23 wb1.cell.cell (2,2, ' pass2 ')#Write the data of 'pass2' to the second row and second column of the sheet 24 wb.save ( " mylogintest.xlsx " ) #Save
The write result is as follows: