Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Excel to HTML (keeping format)

Tags:

python

excel

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.

like image 927
lgds Avatar asked Dec 10 '25 18:12

lgds


1 Answers

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
                              }
like image 123
Tran Hoa Avatar answered Dec 12 '25 08:12

Tran Hoa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!