7

I am trying to save an existing Excel file to HTML in Python using win32com.client. Below is my code and the resulting error message. Any suggestions?

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)

With the following Traceback:

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)
2
  • import win32com.client as win32 excel = win32.gencache.EnsureDispatch('Excel.Application') wb = excel.Workbooks.Open(r'D:\eclipse\atest.xlsx') excel.Visible = True ws = wb.Worksheets('Sheet1') wb.PublishObjects.Add(SourceType=constants.xlSourceRange,Filename=r'D:\eclipse\atest1.html',Sheet='Sheet1',Source='$A$1:$F$6',HtmlType=constants.xlHtmlStatic, DivID='xxx1') wb.PublishObjects(1).Publish(True) Commented Oct 12, 2015 at 6:09
  • using below code successfully save excel to html file. fyi. Commented Oct 12, 2015 at 6:10

2 Answers 2

3

Pandas is very good for this.

Using pandas should simplify this for you. See below:

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 

Example Excel input:

Example html output:

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

But what about images stored in the excel
This only saves dataframe. Not formatting and other info
0

Use xlsx2html module which will retain the cell formatting information as well. link:https://pypi.org/project/xlsx2html/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.