简体   繁体   English

在 Python 3 中将 XLS 文件转换为 XLSX?

[英]Convert XLS files to XLSX in Python 3?

My objective is to take a large number of XLS files and convert them to XLSX.我的目标是获取大量 XLS 文件并将它们转换为 XLSX。 I can do it one line at a time with a simple operation, or do a large number of files in a batch if anyone knows a way.我可以通过简单的操作一次完成一行,或者如果有人知道方法的话,可以批量处理大量文件。

I attempted to use most of the solutions on this Stack post , but it seems they no longer work, or apply. 我尝试使用此 Stack post 上的大多数解决方案,但似乎它们不再起作用或适用。 It is 9 years old, so a modern solution may be needed.它已有 9 年历史,因此可能需要现代解决方案。

Code I tried:我试过的代码:

p.save_book_as(file_name='C://Users//user1//Downloads//oldfile.xls',
           dest_file_name='C://Users//user1//Downloads//oldfile.xlsx')

But this resulted in an error:但这导致了一个错误:

 raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'<?xml ve'

You could try Tablib , as in:您可以尝试Tablib ,如:

with open('input.xls', 'r') as input:
    data = Dataset().load(input)
    with open('output.xlsx', 'wb') as output:
        output.write(data.export('xlsx'))

Not sure if it supports your versions of xls/xlsx, though.不过,不确定它是否支持您的 xls/xlsx 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM