docx, Xlsxwriter Library, using Python to manipulate docx, xlsx format files

Source: Internet
Author: User

Need to generate the corresponding DOCX,XLSX file from the database read log to make corresponding records

So it's natural to use these two libraries.

However, these libraries are widely used, and any effort to perform repetitive operations on Word,excel files can be done using Python to help us

Xlsxwriter Library

  article Structure :

First, Xlsxwriter basic usage, add data to the xlsx file

Second, xlsxwriter format processing, the data to be added to the appropriate format, added to the xlsx file

Three

  First, Xlsxwriter basic usage, add data to the xlsx file

Xlsxwriter can manipulate xlsx format files

Note: Xlsxwriter can only create new files and cannot modify existing files. If you create a new file with the same name as the original file, the original file is overwritten

Installation: sudo pip install Xlsxwriter

1 #!/usr/bin/python2 #Coding:utf-83 4 ImportXlsxwriter5 6 #Create a new xlsx file (if the original file with the same name is overwritten)7Workbook = Xlsxwriter. Workbook ("expenses01.xlsx")8 9 #create a new form with the default name of "Sheet1" and enter a character parameter to specify a nameTenWorksheet =Workbook.add_worksheet () One  AExpenses = ( -['Rent', 1000], -[' Gas', 100], the[' Food', 300], -['Gym', 50], - ) -  + #worksheet default is counting from 0 rows, 0 columns -row =0 +Col =0 A  at #The Worksheet.write method writes data to the XLSX table - #The parameters are: line number, column number, data -  forItem, Costinch(expenses): - worksheet.write (Row, col, item) -Worksheet.write (Row, col + 1, cost) -Row + = 1 in  - #explicitly close the workbook, if not explicitly specified, then automatically close when the scope ends toWorkbook.close ()

Effect Show:

docx, Xlsxwriter Library, using Python to manipulate docx, xlsx format files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.