3

I am trying to convert an Excel file to an HTML file while keeping the format of the workbook.

enter image description here

Using Excel, I am able to switch from xlsx to htm: File -> Save as -> Web page (*.html, *.htm)

enter image description here

Using Python, I am always getting something gibberish like the below image as workbook.htm or workbook.html.

enter image description here

import xlwings as xw
file_path = "*.xlsx"
excel_app = xw.App(visible=False)
wb = excel_app.books.open(file_path)
wb.save("*.html")
wb.save("*.htm")
from xlsx2html import xlsx2html
xlsx2html('*xlsx', '*.htm')
xlsx2html('*xlsx', '*.html')

I have used dummy files, I am just trying to go from the xlsx file to the htm/hmtl file using Python and keeping the format, e.g. background colors, borders, etc.

1 Answer 1

2

I used to have such problem. I also used xlwings library, customized it and success. You find and edit in the file xlwings/_xlwindows.py as follows:

def save(self, path=None):
    saved_path = self.xl.Path
    source_ext = os.path.splitext(self.name)[1] if saved_path else None
    target_ext = os.path.splitext(path)[1] if path else '.xlsx'
    if saved_path and source_ext == target_ext:
        file_format = self.xl.FileFormat
    else:
        ext_to_file_format = {'.xlsx': FileFormat.xlOpenXMLWorkbook,
                              '.xlsm': FileFormat.xlOpenXMLWorkbookMacroEnabled,
                              '.xlsb': FileFormat.xlExcel12,
                              '.xltm': FileFormat.xlOpenXMLTemplateMacroEnabled,
                              '.xltx': FileFormat.xlOpenXMLTemplateMacroEnabled,
                              '.xlam': FileFormat.xlOpenXMLAddIn,
                              '.xls': FileFormat.xlWorkbookNormal,
                              '.xlt': FileFormat.xlTemplate,
                              '.xla': FileFormat.xlAddIn,
                              '.html': FileFormat.xlHtml # ---> add new
                              }
Sign up to request clarification or add additional context in comments.

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.