📅  最后修改于: 2023-12-03 15:00:39.078000             🧑  作者: Mango
Excel is an indispensable tool in the business world. Excelwriter Python is a Python module that allows programming in Python to create and modify Excel files (*.xlsx).
The easiest way to install is using pip:
pip install xlsxwriter
import xlsxwriter
workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')
worksheet.write('B1', 'World')
workbook.close()
In the above example, we created a new Excel file named "example.xlsx" and added a new worksheet to it. We wrote "Hello" to cell A1 and "World" to cell B1.
With Excelwriter Python, you can also:
Here's an example of formatting cells:
import xlsxwriter
workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
money = workbook.add_format({'num_format': '$#,##0'})
worksheet.write('A1', 'Hello', bold)
worksheet.write('B1', 'World', bold)
worksheet.write('C1', 1234.56, money)
worksheet.write('D1', '=SUM(C1:C10)')
workbook.close()
This time, we made "Hello" and "World" bold and formatted the value 1234.56 as a currency with the $#,##0
format. We also calculated the sum of the first ten values in column C using a formula.
Excelwriter Python is a powerful tool for generating and modifying Excel files in Python. It provides a high degree of flexibility and control over the formatting and content of Excel files. With the ability to automate tedious Excel tasks, Excelwriter Python can save a lot of time and effort for Python developers.