简体   繁体   English

如何通过python脚本将.xls文件转换为pdf

[英]How to convert .xls file to pdf through python scripting

I have created .xls using xlwt module. 我已经使用xlwt模块创建了.xls。 Now i want to convert this newly created xls file to pdf. 现在,我要将这个新创建的xls文件转换为pdf。 Can anyone please tell me how can i do this through python scripting. 谁能告诉我如何通过python脚本来做到这一点。 Am using python 2.6 version. 我正在使用python 2.6版本。

There are at least two possibilities here: either you read the .xls back in as cells and generate a PDF, or maybe you want to print the .xls to .pdf from the program that just created the .xls . 这里至少存在两种​​可能性:要么将.xls作为单元重新读回并生成PDF,要么也许要从刚刚创建.xls的程序中将.xls打印为.pdf

In the first case you might want to consider writing the .pdf directly (you have the data to make the .xls in the program at some point). 在第一种情况下,您可能要考虑直接编写.pdf (您有数据可以在程序中随时将.xls编写)。 I have a grid class that can be written out in HTML format, or to .xls' (using xlwt ), .xlsm (using openpyxl ) and to .pdf (using reportlab`). 我有一个网格类,可以以HTML格式写出,也可以写为.xls' (using xlwt ), 、. xlsm (using openpyxl ) and to .pdf (using reportlab`)。

You will need to use the second option if you write formula and that kind of stuff in your .xls and want to have the calculated results in your .pdf . 如果您在.xls编写公式和此类内容,并希望在.pdf包含计算结果,则需要使用第二个选项。 In that case use subprocess to call Microsoft Excel or OpenOffice/LibreOffice Calc with the right commandline parameters for printing/converting the file to PDF. 在这种情况下,请使用子过程来调用带有正确的命令行参数的Microsoft ExcelOpenOffice/LibreOffice Calc ,以将文件打印/转换为PDF。

Eg for LibreOffice 4.0 the commandline would be: 例如,对于LibreOffice 4.0,命令行为:

scalc --convert-to pdf yourfile.xls

which will result in a yourfile.pdf 这将导致一个yourfile.pdf

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

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