Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
959 views
in Technique[技术] by (71.8m points)

excel - .xlsx and xls(Latest Versions) to pdf using python

With the help of this .doc to pdf using python Link I am trying for excel (.xlsx and xls formats)

Following is modified Code for 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.
'

for files in os.listdir("."):
    if files.endswith(".xlsx"):
        print files

print '

'

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:ExcelXlsxWriter-0.5.1PDF_excel created.
apms_trial.xlsx
~$apms_trial.xlsx

Exporting C:ExcelXlsxWriter-0.5.1PDF_excelapms_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)

What should be FileFormat file format? Please Help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Link of xlsxwriter :

https://xlsxwriter.readthedocs.org/en/latest/contents.html

With the help of this you can generate excel file with .xlsx and .xls

for example excel file generated name is trial.xls

Now if you want to generate pdf of that excel file then do the following :

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...