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

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

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

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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With