我正在尝试使用 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/