С помощью этой ссылки .doc в pdf с использованием python я пытаюсь использовать Excel ( форматы .xlsx и xls)
Ниже приведен измененный код для 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()
Показывает следующую ошибку:
>>> 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)
>>>
Есть проблема в
doc.SaveAs(out_file, FileFormat=56)
Какой должен быть формат файла FileFormat? Пожалуйста помоги
~$
. Когда вы пытаетесь преобразовать временный файл, он жалуется. Попробуйте закрыть Excel, если он у вас открыт, или удалить этот файл, или проигнорировать его с помощью инструкцииif
. - person darthbith   schedule 31.12.2013out_name = files.replace(file_type, r"pdf")
, потому что вы никогда не заменяете тип файлаxls
наpdf
, вы заменяете толькоxlsx
, поэтому, если вы когда-нибудь попытаетесь преобразовать файлxls
, он перезапишет существующий. - person darthbith   schedule 31.12.2013if ((files.endswith(".xlsx") or files.endswith('xls')) and files[0] != "~"):
- person mattvivier   schedule 30.10.2015