xlsxwriter Is used to create Excel XLSX Of documents Python modular , Can be used to put text 、 Numbers 、 Formulas and hyperlinks write Excel2007+ Multiple worksheets in the file . It supports formatting and other functions .
The main advantage over other libraries is :
But there is also one biggest disadvantage :
Read operation is not supported , Combined reading required Excel The library of ;
Used in conjunction with libraries that read files , The write operation can preserve the source file format ;
The installation is relatively simple , Open the command line tool , Enter the command
pip install xlsxwriter
1、xlsxwriter The official document address of : ad locum
2、 Easy to use :
Import module first :import xlxswrite as xw
Create a xlsx File workbooks : wb = xw.WorkBookk(‘test.xlsx’)
Create a... In the workbook sheet surface sheet = wb.add_worksheet()
stay sheet Write cell data to the table sheet.write(‘A1’,‘one’)
Save and file wb.close()
The above is a simple write operation , When writing cells , You can specify coordinates , You can also write formulas ;
def demo(): '''''' # Create a workbook wb = xw.Workbook('H://writer.xlsx') # Add one sheet sheet = wb.add_worksheet() # Customize sheet name The default is sheet1 sheet2... sheet = wb.add_worksheet('Name') # Write cell data sheet.write('A1','one') # Pass in row and col from 0 Start sheet.write(2,2,' coordinate ') sheet.write('A2',1) sheet.write('B2',2) # Write the formula sheet.write('C2','=sum(A2:B2)') # Save and close wb.close()
3、 Write the specified format :
stay xlwxwriter in , Handle write Method can write any data , There are also specified methods to write data in the specified format , Here are some common methods :
write_string()
write_number()
write_blank()
write_formula()
write_datetime()
write_boolean()
write_url()
4、 Write style :
xlxswriter When writing data , You can also add styles to cells ;