Home  >  Article  >  Backend Development  >  Use python to implement XlsxWriter to create and edit Excel files

Use python to implement XlsxWriter to create and edit Excel files

不言
不言Original
2018-05-04 14:45:534203browse

This article mainly introduces the use of XlsxWriter to create and edit Excel files. It has certain reference value. Now I share it with you. Friends in need can refer to it.

I have used other tools before operating Excel. Method, for Excel writing, the xlwt module has been used, and the win32com interface has also been used directly. If you say which one can best exert the power of Excel, then it must be the way of implementing the win32com interface. However, this method requires the support of Office software.

As for xlwt, I think the functions we usually use are basically available, and usually this can basically meet my personal requirements.

I used the XlsxWriter module today, but I haven’t found any special functions yet. Of course, I just copied a demo program. First show the program code

#!/usr/bin/python
# -*- coding: cp936 -*-
 
import xlsxwriter
 
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet('demo')
 
worksheet.set_column('A:A',20)
bold = workbook.add_format({'bold':True})
 
worksheet.write('A1','Hello')
worksheet.write('A2','WOrld',bold)
worksheet.write('B2',u'你好!',bold)
 
worksheet.write(2,0,32)
worksheet.write(3,0,35.5)
worksheet.write(4,0,'=SUM(A3:A4)')
 
workbook.close()

After the program is run, a file named demo.xlsx is generated. The content after opening is as follows:

By editing The operation creates a Sheet with the specified name and writes relevant information. A sum method is also called to calculate the sum of the values ​​of the two cells.

Compared with the xlwt module used before, the XlsxWriter module is much larger. From the huge size, you can roughly guess that the function of this module should be more powerful. We can get some clues from the file format support. This module supports xlsx format files that xlwt does not support. As for other functions, you can really understand them later and then do a more detailed study.

Related recommendations:

A simple analysis of the xlsxwriter library in python

The above is the detailed content of Use python to implement XlsxWriter to create and edit Excel files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn