.xlsx 和 xls(最新版本)使用 python 转换为 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20854840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.xlsx and xls(Latest Versions) to pdf using python
提问by eegloo
With the help of this .doc to pdf using pythonLink I am trying for excel (.xlsx and xls formats)
在这个.doc to pdf的帮助下,使用 pythonLink 我正在尝试使用 excel(.xlsx 和 xls 格式)
Following is modified Code for Excel:
以下是修改后的 Excel 代码:
import os
from win32com import client
folder = "C:\Oprance\Excel\XlsxWriter-0.5.1"
file_type = 'xlsx'
out_folder = folder + "\PDF_excel"
os.chdir(folder)
if not os.path.exists(out_folder):
print 'Creating output folder...'
os.makedirs(out_folder)
print out_folder, 'created.'
else:
print out_folder, 'already exists.\n'
for files in os.listdir("."):
if files.endswith(".xlsx"):
print files
print '\n\n'
word = client.DispatchEx("Excel.Application")
for files in os.listdir("."):
if files.endswith(".xlsx") or files.endswith('xls'):
out_name = files.replace(file_type, r"pdf")
in_file = os.path.abspath(folder + "\" + files)
out_file = os.path.abspath(out_folder + "\" + out_name)
doc = word.Workbooks.Open(in_file)
print 'Exporting', out_file
doc.SaveAs(out_file, FileFormat=56)
doc.Close()
It is showing following error :
它显示以下错误:
>>> execfile('excel_to_pdf.py')
Creating output folder...
C:\Excel\XlsxWriter-0.5.1\PDF_excel created.
apms_trial.xlsx
~$apms_trial.xlsx
Exporting C:\Excel\XlsxWriter-0.5.1\PDF_excel\apms_trial.pdf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "excel_to_pdf.py", line 30, in <module>
doc = word.Workbooks.Open(in_file)
File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel
', u"Excel cannot open the file '~$apms_trial.xlsx' because the file format or f
ile extension is not valid. Verify that the file has not been corrupted and that
the file extension matches the format of the file.", u'xlmain11.chm', 0, -21468
27284), None)
>>>
There is problem in
有问题
doc.SaveAs(out_file, FileFormat=56)
doc.SaveAs(out_file, FileFormat=56)
What should be FileFormatfile format? Please Help
FileFormat文件格式应该是什么?请帮忙
回答by eegloo
Link of xlsxwriter :
xlsxwriter 的链接:
https://xlsxwriter.readthedocs.org/en/latest/contents.html
https://xlsxwriter.readthedocs.org/en/latest/contents.html
With the help of this you can generate excel file with .xlsxand .xls
借助此功能,您可以使用.xlsx和.xls生成 excel 文件
for example excel file generated name is trial.xls
例如excel文件生成的名称是trial.xls
Now if you want to generate pdf of that excel file then do the following :
现在,如果您想生成该 excel 文件的 pdf,请执行以下操作:
from win32com import client
xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open('C:\excel\trial.xls')
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, 'C:\excel\trial.pdf')
回答by lxx
You can print an excel sheet to pdf on linux using python. Do need to run openoffice as a headless server and use unoconv, takes a bit of configuring but is doable
您可以使用 python 在 linux 上将 excel 表打印为 pdf。确实需要将 openoffice 作为无头服务器运行并使用 unoconv,需要一些配置但可行
You run OO as a (service) daemon and use it for the conversions for xls, xlsx and doc, docx.
您将 OO 作为(服务)守护程序运行,并将其用于 xls、xlsx 和 doc、docx 的转换。
回答by daansteraan
I got the same thing and the same error... ANSWER: 57.... see below...
我得到了同样的事情和同样的错误......答案:57......见下文......
from win32com import client
import win32api
def exceltopdf(doc):
excel = client.DispatchEx("Excel.Application")
excel.Visible = 0
wb = excel.Workbooks.Open(doc)
ws = wb.Worksheets[1]
try:
wb.SaveAs('c:\targetfolder\result.pdf', FileFormat=57)
except Exception, e:
print "Failed to convert"
print str(e)
finally:
wb.Close()
excel.Quit()
... as an alternative to the fragile ExportAsFixedFormat...
...作为脆弱的ExportAsFixedFormat的替代...

