Excel to HTML using Python

The use of Python Pandas Library to convert data from an Excel Spreadsheet to an HTML Page

Razni Hashim
2 min readMar 31, 2023
AI art generated by Bing

I asked ChatGPT to give me a code to convert data from an Excel to an HTML page. It gave a code. But it had some flaws. Then, I asked it to generate a corrected code after I myself typed the flaws.

After having several chats with ChatGPT I was able to generate a code that converted data from an Excel to an HTML page. It is just a simple process. But there are certain people who always use Excel. Some might want to convert Excel files to HTML files frequently. So there’s a simple Python code that can be used to convert the Excel file to an HTML file within a few seconds.

But you must first save an HTML file using notepad or any other editor using the extension ‘.html’ at the end.

import pandas as pd

# Load data from Excel file
data = pd.read_excel('#Excel_File_Path.xlsx')

# Write data to HTML file with formatting
with open('#HTML_File_Path.html', 'w') as f:
f.write(data.to_html(index=False, classes='table', header='true', border=1, justify='left'))

Then you can use the above code. Note that you must replace the #Excel_File_Path name using your Excel file path. Make sure that the path is in inverted commas with the extension xlsx.

Then you must replace the #HTML_File_Path name using your HTML file path that you saved earlier. Next save this Python program and run the program.

Always remember to save the Python program and then run the program.

Now open the HTML file using a web browser. You will be able to see that the data in the excel file have been converted to an html file.

I have attached a screenshot of the excel file I tested and the html file of generated by the code below.

You also can try this code and let me know if it works for you. I would be glad to get some feedbacks to improve this code further.

If you have any issues please feel free to contact me!

Have a good day!

Excel file
HTML file

--

--