简体   繁体   English

在python中解压.xls文件

[英]Decompress .xls file in python

I'm searching for a method to decompress/unzip .xls files in python.我正在寻找一种在 python 中解压缩/解压缩 .xls 文件的方法。 By opening Excel files with 7-Zip you can see the directories I'd like to extract.通过使用 7-Zip 打开 Excel 文件,您可以看到我想要提取的目录。

I already tried renaming the Excel to ".zip" and then extracting it我已经尝试将 Excel 重命名为“.zip”,然后将其解压缩

myExcelFile = zipfile.ZipFile("myExcel.zip") 
myExcelFile.extractall()

but it throws但它抛出

zipfile.BadZipFile: File is not a zip file

.xls in 7-Zip 7-Zip 格式的 .xls

.xls files use the BIFF format. .xls 文件使用 BIFF 格式。 .xlsx files use Office Open XML, which is a zipped XML format. .xlsx 文件使用 Office Open XML,这是一种压缩的 XML 格式。 BIFF is not a zipped format; BIFF 不是压缩格式; files using that format are not recognized by zip libraries. zip 库无法识别使用该格式的文件。 – shmee – shmee

a conversion to .xlsx is the solution转换为 .xlsx 是解决方案

import win32com.client as win32
fname = "full+path+to+xls_file"
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(fname)

wb.SaveAs(fname+"x", FileFormat = 51)    #FileFormat = 51 is for .xlsx extension
wb.Close()                               #FileFormat = 56 is for .xls extension
excel.Application.Quit()

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

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