python - 在 Python 中将 Excel 另存为 HTML

标签 python excel

我正在尝试使用 win32com.client 将现有的 Excel 文件保存为 Python 中的 HTML。下面是我的代码和生成的错误消息。有什么建议么?

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'D:\eclipse\test.xlsx')
excel.Visible = True
ws = wb.Worksheets('Sheet1')
ob = wb.PublishObjects.Add(1,'C:\test.html','Sheet1')
ob.Publish(True)

具有以下回溯:

Traceback (most recent call last):  
File "D:\eclipse\DMS\AGADMS\exceltohtml.py", line 21, in <module>  
ob = wb.PublishObjects.Add(1,'C:\test.html')  
File "C:\Python34\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x7\PublishObjects.py", line 37, in Add
    , Title)  
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)

最佳答案

Pandas 非常适合这个。

使用 pandas 应该可以为您简化这一过程。见下文:

import pandas as pd 

wb = pd.read_excel('D:\eclipse\test.xlsx') # This reads in your excel doc as a pandas DataFrame

wb.to_html('C:\test.html') # Export the DataFrame (Excel doc) to an html file 

示例 Excel 输入:

示例 html 输出:

希望这对您有所帮助。

关于python - 在 Python 中将 Excel 另存为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073793/

相关文章:

excel - VBA错误处理程序导致错误时该怎么办?

excel - Powershell 中的 VLOOKUP 脚本

python - 十进制转二进制,如何得到8位二进制?

python - 无法访问 PySpark 中的 RowMatrix 方法 : columnSimilarities(), computeColumnSummaryStatistics()

python - 从代码中获取所有 href

javascript - jQuery 电子表格/网格插件,可从 Excel 复制/粘贴到 Excel

python - 如何使用 ctypes 将 Python 列表转换为 C 数组?

python - 使用循环将 CSV 数据提取到对象中

excel - 使用IF语句检查值

javascript - JS : Create report on Node. js