1 There are xlrd, XLWT, Xlutils, OPENPYXL and Xlsxwriter in the reading and writing of Excel in Python. 2 31. XLRD is primarily used to read Excel files4 5 Importxlrd6 7workbook = Xlrd.open_workbook (u'fun loading daily data and trends. XLS')8 9sheet_names=workbook.sheet_names ()Ten One forSheet_nameinchSheet_names: A -Sheet2 =workbook.sheet_by_name (sheet_name) - the PrintSheet_name rows = sheet2.row_values (3)#get the fourth line of content - -cols = sheet2.col_values (1)#get second column content - + Printrows - + Printcols A at - -2. XLWT is primarily used to write Excel files - - ImportXLWT - inWBK =XLWT. Workbook () - toSheet = Wbk.add_sheet ('Sheet 1') + -Sheet.write (0,1,'Test Text')#The first column of line No. 0 writes the content the *Wbk.save ('Test.xls') $ Panax Notoginseng - the3. Xlutils combined with XLRD can be used to modify the Excel file Purpose + A Importxlrd the + fromXlutils.copyImportCopy - $workbook = Xlrd.open_workbook (u'fun loading daily data and trends. XLS') $ -Workbooknew =copy (Workbook) - theWS =workbooknew.get_sheet (0) - WuyiWs.write (3, 0,'changed!') the -Workbooknew.save (U'fun loading daily data and trends Copy.xls') Wu - About $4. OPENPYXL can read and write to an Excel file - - fromOpenpyxlImportWorkbook - A fromOpenpyxlImportLoad_workbook + the fromOpenpyxl.writer.excelImportExcelwriter - $ the theWorkbook_ = Load_workbook (u"New Song retrieval failed 1477881109469.xlsx") the theSheetnames =workbook_.get_sheet_names ()#get the name of the form - in PrintSheetnames the theSheet =Workbook_.get_sheet_by_name (sheetnames[0]) About the PrintSheet.cell (row=3,column=3). Value the thesheet['A1'] =' -' + -Workbook_.save (U"New Song retrieval failed 1477881109469_new.xlsx") the BayiWB =Workbook () the theWS =wb.active - -ws['A1'] = 4 the theWb.save ("New song retrieval failed. xlsx") the the - the5. Xlsxwriter can write Excel files and add tables the the ImportXlsxwriter94 the the the defGet_chart (series):98 AboutChart = Workbook.add_chart ({'type':' Line'}) - 101 forSesinchSeries:102 103Name = ses["name"]104 theValues = ses["Values"]106 107 chart.add_series ({108 109 'name': Name, the 111 'Categories':'a2:a10', the 113 'Values': Values the the }) the 117Chart.set_size ({'width': 700,'Height': 350}) 118 119 returnChart - 121 122 123 if __name__=='__main__':124 theWorkbook = Xlsxwriter. Workbook (U'key data and trends for the H5 Application Center. xlsx') 126 127worksheet = Workbook.add_worksheet (u"Daily Pv,uv") - 129headings = ['Date','Average'] the 131Worksheet.write_row ('A1', headings) the 133index=0134 135 forRowinchRange (1,10):136 137 forCominch[0,1]:138 139 worksheet.write (Row,com,index) $ 141Index+=1142 143Series = [{"name":"Average","Values":"B2:B10"}]144 145Chart =Get_chart (series)146 147Chart.set_title ({'name':'daily page sharing data'}) 148 149Worksheet.insert_chart ('H7', chart) Max 151Workbook.close ()
Several ways that Python operates Excel