在Python中是否可以将下面奇怪的.XLS文件(实际上是某种HTML/XML格式)转换为.XLSX?

2024-09-27 04:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这些.xls文件的格式让我很困惑,因为它们不是真正的.xls文件,我把文件的前几行放在下面作为参考,full file here

转换normal.xls对p.save_book_as(file_name=fname, dest_file_name=fname+'x')没有问题

我想用python批量转换成.xlsx,下面的格式可以吗

MIME-Version: 1.0
X-Document-Type: Workbook
Content-Type: multipart/related; boundary="----=_NextPart_86ab7b61_9054_45ca_a3a6_49bc8ebc61db"

This document is a Single File Web Page, also known as a Web Archive file.  If you are seeing this message, your browser or editor doesn't support Web Archive files.  Please download a browser that supports Web Archive, such as Microsoft Internet Explorer.

------=_NextPart_86ab7b61_9054_45ca_a3a6_49bc8ebc61db
Content-Location: file:///C:/86ab7b61_9054_45ca_a3a6_49bc8ebc61db/Workbook.html
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="us-ascii"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-microsoft-com:office:office" xmlns:x=3D"urn:schemas-microsoft-com:office:excel" xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<meta name=3D"Excel Workbook Frameset">

<meta name=3DProgId content=3DExcel.Sheet>
<link rel=3DFile-List href=3D"Worksheets/filelist.xml">

<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>

Tags: 文件namewebhtmlastypecontentxls
1条回答
网友
1楼 · 发布于 2024-09-27 04:20:12

这似乎是“Excel compatible HTML”。 虽然我不知道纯python转换器,但您可以尝试使用excel作为外部转换器,即打开这些文件并将其保存到xlsx,如described here并复制到下面。这需要pywin32软件包才能远程访问excel

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

相关问题 更多 >

    热门问题