python xlsx转xls_使用python将.xlsx和xls(最新版本)转换为pdf

借助这个.doc to pdf using 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 "", line 1, in

File "excel_to_pdf.py", line 30, in

doc = word.Workbooks.Open(in_file)

File ">", 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)

>>>

里面有问题

文档另存为(out_file,FileFormat=56)

文件格式应该是什么?

请帮忙

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python中的openpyxl库将xls文件转换xlsx文件。具体步骤如下: 1. 首先安装openpyxl库,可以使用命令行输入以下命令进行安装: ``` pip install openpyxl ``` 2. 导入openpyxl库和xlrd库,使用xlrd库可以读取xls文件中的数据: ```python import openpyxl import xlrd ``` 3. 使用xlrd库读取xls文件中的数据,并创建一个新的xlsx文件: ```python # 打开xls文件 workbook = xlrd.open_workbook('example.xls') # 获取xls文件中第一个sheet sheet = workbook.sheet_by_index(0) # 创建一个新的xlsx文件 new_workbook = openpyxl.Workbook() new_sheet = new_workbook.active ``` 4. 将xls文件中的数据写入新的xlsx文件中: ```python # 将xls文件中的数据写入新的xlsx文件中 for row_index in range(sheet.nrows): for col_index in range(sheet.ncols): new_sheet.cell(row=row_index+1, column=col_index+1, value=str(sheet.cell_value(row_index, col_index))) ``` 5. 最后保存新的xlsx文件: ```python # 保存新的xlsx文件 new_workbook.save('example.xlsx') ``` 完整代码如下: ```python import openpyxl import xlrd # 打开xls文件 workbook = xlrd.open_workbook('example.xls') # 获取xls文件中第一个sheet sheet = workbook.sheet_by_index(0) # 创建一个新的xlsx文件 new_workbook = openpyxl.Workbook() new_sheet = new_workbook.active # 将xls文件中的数据写入新的xlsx文件中 for row_index in range(sheet.nrows): for col_index in range(sheet.ncols): new_sheet.cell(row=row_index+1, column=col_index+1, value=str(sheet.cell_value(row_index, col_index))) # 保存新的xlsx文件 new_workbook.save('example.xlsx') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值