.xlsx 和 xls(最新版本)使用 python 转换为 pdf [英] .xlsx and xls(Latest Versions) to pdf using python

查看:45
本文介绍了.xlsx 和 xls(最新版本)使用 python 转换为 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助此.doc to pdf using python链接我正在尝试使用 excel(.xlsx 和 xls 格式)

以下是修改后的 Excel 代码:

导入操作系统从 win32com 导入客户端folder = "C:\Oprance\Excel\XlsxWriter-0.5.1"文件类型 = 'xlsx'out_folder = 文件夹 + "\PDF_excel"os.chdir(文件夹)如果不是 os.path.exists(out_folder):打印正在创建输出文件夹..."os.makedirs(out_folder)打印 out_folder, '创建.别的:print out_folder, '已经存在.
'对于 os.listdir(".") 中的文件:如果 files.endswith(".xlsx"):打印文件打印 '

'word = client.DispatchEx("Excel.Application")对于 os.listdir(".") 中的文件:如果 files.endswith(".xlsx") 或 files.endswith('xls'):out_name = files.replace(file_type, r"pdf")in_file = os.path.abspath(文件夹 + "\" + 文件)out_file = os.path.abspath(out_folder + "\" + out_name)doc = word.Workbooks.Open(in_file)打印导出",out_filedoc.SaveAs(out_file, FileFormat=56)doc.Close()

它显示以下错误:

<预><代码>>>>execfile('excel_to_pdf.py')正在创建输出文件夹...C:ExcelXlsxWriter-0.5.1PDF_excel 已创建.apms_trial.xlsx~$apms_trial.xlsx导出 C:ExcelXlsxWriter-0.5.1PDF_excelapms_trial.pdf回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件excel_to_pdf.py",第 30 行,在 <module> 中doc = word.Workbooks.Open(in_file)文件<COMObject<unknown>>",第 8 行,打开pywintypes.com_error: (-2147352567, '发生异常.', (0, u'Microsoft Excel', u"Excel 无法打开文件 '~$apms_trial.xlsx' 因为文件格式或 f文件扩展名无效.确认文件没有损坏并且文件扩展名与文件格式匹配.", u'xlmain11.chm', 0, -2146827284),无)>>>

存在问题

doc.SaveAs(out_file, FileFormat=56)

FileFormat 文件格式应该是什么?请帮忙

解决方案

xlsxwriter 链接:

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

借助此工具,您可以生成带有 .xlsx.xls

的 excel 文件

例如excel文件生成的名字是trial.xls

现在,如果您想生成该 excel 文件的 pdf,请执行以下操作:

from win32com 导入客户端xlApp = client.Dispatch("Excel.Application")book = xlApp.Workbooks.Open('C:\excel\trial.xls')ws = book.Worksheets[0]ws.Visible = 1ws.ExportAsFixedFormat(0, 'C:\excel\trial.pdf')

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

解决方案

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

这篇关于.xlsx 和 xls(最新版本)使用 python 转换为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆