简体   繁体   English

Python:写入 Excel 2007+ 文件(.xlsx 文件)

[英]Python: Writing to Excel 2007+ files (.xlsx files)

Is there a Python module that writes Excel 2007+ files?有写Excel 2007+文件的Python模块吗?
I'm interested in writing a file longer than 65535 lines and only Excel 2007+ supports it.我有兴趣编写一个超过 65535 行的文件,只有 Excel 2007+ 支持它。

Take a look at Eric' Gazoni's openpyxl project. 看看Eric'Gazoni的openpyxl项目。 The code can be found on bitbucket . 代码可以在bitbucket上找到。

There are two libraries you can take a look at. 您可以查看两个库。

Python-xlsx and PyXLSX Python-xlsxPyXLSX

EDIT: As the comments mention, for writing you check out openpyxl 编辑:正如评论中提到的,写作时请查看openpyxl

You should take a look at xlsxcessive . 你应该看看xlsxcessive It's for writing xlsx files, and is, perhaps, a bit more pythonic. 它是用于编写xlsx文件,也许是更加pythonic。

XlsxWriter Python模块编写了2007+ xlsx文件。

If you are on Windows and have Excel 2007+ installed, you should be able to use pywin32 and COM to write XLSX files using almost the same code as you would would to write XLS files ... just change the "save as ...." part at the end. 如果你在Windows上安装了Excel 2007+,你应该可以使用pywin32和COM来编写XLSX文件,使用与编写XLS文件几乎相同的代码...只需更改“另存为... “最后的一部分。

Probably, you can also write XLSX files using Excel 2003 with the freely downloadable add-on kit but the number of rows per sheet would be limited to 64K. 也许,您也可以使用Excel 2003使用可免费下载的附加工具包编写XLSX文件,但每张工作表的行数限制为64K。

This should give an idea how to do that:这应该给出一个想法如何做到这一点:

import xlsxwriter

workbook = xlsxwriter.Workbook("output_file.xlsx"))
# new sheet
worksheet = workbook.add_worksheet("sheet_name")
# Add a number format: 3 digits precision
precision = workbook.add_format({'num_format': '0.000'})

for(iterate your data):
    worksheet.write(row, col, title)
    worksheet.write_number(row, col, value, precision)
    ...

workbook.close()

Pyvot: http ://packages.python.org/Pyvot/tutorial.html,虽然它仅适用于Excel 2010+

So you want to write xlsx file, into my mind the Microsoft.office.excel.interop dll come to my mind, but not use it on a server. 所以你想编写xlsx文件,我想到了Microsoft.office.excel.interop dll,但不是在服务器上使用它。

I know you can call dll from python : http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel(office.11).aspx 我知道你可以从python中调用dll: http//msdn.microsoft.com/en-us/library/microsoft.office.interop.excel(office.11​​).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM