程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python office automation writing excel operation using module xlsxwriter

編輯:Python

xlsxwriter Library Introduction :

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 :

  • It supports more than any other module Excel characteristic .
  • It's right Excel The generated file has high fidelity . in the majority of cases , Generated files 100% Equate to Excel Generated files .
  • It has a lot of documentation 、 Sample files and tests .
  • It's fast , Can be configured to use very little memory , Even very large output files .

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 ;

Module installation :

The installation is relatively simple , Open the command line tool , Enter the command

pip install xlsxwriter

Library usage :

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 ;


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved