Python|使用 xlsxwriter 模块创建和写入 excel 文件
XlsxWriter是一个Python模块,用于以 XLSX 文件格式写入文件。它可用于将文本、数字和公式写入多个工作表。此外,它还支持格式设置、图像、图表、页面设置、自动过滤器、条件格式和许多其他功能。
使用此命令安装 xlsxwriter 模块:
pip install xlsxwriter
注意:在整个 XlsxWriter 中,行和列都是零索引的。工作表中的第一个单元格,A1 是 (0, 0),B1 是 (0, 1),A2 是 (1, 0),B2 是 (1, 1) ..所有类似的。
让我们看看如何使用Python创建和写入 excel-sheet。
代码 #1:使用 A1 表示法(单元格名称)在特定单元格中写入数据。
Python3
# import xlsxwriter module
import xlsxwriter
# Workbook() takes one, non-optional, argument
# which is the filename that we want to create.
workbook = xlsxwriter.Workbook('hello.xlsx')
# The workbook object is then used to add new
# worksheet via the add_worksheet() method.
worksheet = workbook.add_worksheet()
# Use the worksheet object to write
# data via the write() method.
worksheet.write('A1', 'Hello..')
worksheet.write('B1', 'Geeks')
worksheet.write('C1', 'For')
worksheet.write('D1', 'Geeks')
# Finally, close the Excel file
# via the close() method.
workbook.close()
Python3
# import xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('Example2.xlsx')
worksheet = workbook.add_worksheet()
# Start from the first cell.
# Rows and columns are zero indexed.
row = 0
column = 0
content = ["ankit", "rahul", "priya", "harshita",
"sumit", "neeraj", "shivam"]
# iterating through content list
for item in content :
# write operation perform
worksheet.write(row, column, item)
# incrementing the value of row by one
# with each iterations.
row += 1
workbook.close()
Python3
# import xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('Example3.xlsx')
# By default worksheet names in the spreadsheet will be
# Sheet1, Sheet2 etc., but we can also specify a name.
worksheet = workbook.add_worksheet("My sheet")
# Some data we want to write to the worksheet.
scores = (
['ankit', 1000],
['rahul', 100],
['priya', 300],
['harshita', 50],
)
# Start from the first cell. Rows and
# columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for name, score in (scores):
worksheet.write(row, col, name)
worksheet.write(row, col + 1, score)
row += 1
workbook.close()
输出:
代码#2:使用行列表示法(索引值)在特定单元格中写入数据。
Python3
# import xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('Example2.xlsx')
worksheet = workbook.add_worksheet()
# Start from the first cell.
# Rows and columns are zero indexed.
row = 0
column = 0
content = ["ankit", "rahul", "priya", "harshita",
"sumit", "neeraj", "shivam"]
# iterating through content list
for item in content :
# write operation perform
worksheet.write(row, column, item)
# incrementing the value of row by one
# with each iterations.
row += 1
workbook.close()
输出:
代码 #3:创建具有特定名称的新工作表
Python3
# import xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('Example3.xlsx')
# By default worksheet names in the spreadsheet will be
# Sheet1, Sheet2 etc., but we can also specify a name.
worksheet = workbook.add_worksheet("My sheet")
# Some data we want to write to the worksheet.
scores = (
['ankit', 1000],
['rahul', 100],
['priya', 300],
['harshita', 50],
)
# Start from the first cell. Rows and
# columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for name, score in (scores):
worksheet.write(row, col, name)
worksheet.write(row, col + 1, score)
row += 1
workbook.close()
输出:
与用于编写 Excel 文件的替代Python模块相比,XlsxWriter 具有一些优点和缺点。
好处:
- 它支持的 Excel 功能比任何替代模块都多。
- 它对 Excel 生成的文件具有高度的保真度。在大多数情况下,生成的文件与 Excel 生成的文件 100% 等效。
- 它有大量的文档、示例文件和测试。
- 它速度很快,即使对于非常大的输出文件,也可以配置为使用很少的内存。
缺点:
- 它无法读取或修改现有的 Excel XLSX 文件。