Convert Excel to HTML with Python
Escape cell text and write a standalone table with a header row.
View code and setup
Before you start
- Install Python 3. Create the environment below, save the Python example as task.py, then run .venv/bin/python task.py in the folder containing input.xlsx. On Windows, use py -m venv .venv and .venv\Scripts\python.exe in place of .venv/bin/python. Replace Inventory with your worksheet name.
python3 -m venv .venv
.venv/bin/python -m pip install openpyxlfrom html import escape
from openpyxl import load_workbook
workbook = load_workbook("input.xlsx", read_only=True, data_only=True)
sheet = workbook["Inventory"]
rows = list(sheet.iter_rows(values_only=True))
workbook.close()
parts = ['<!doctype html><meta charset="utf-8"><title>Inventory</title><table>']
for index, row in enumerate(rows):
tag = "th" if index == 0 else "td"
cells = "".join(f"<{tag}>{escape('' if value is None else str(value))}</{tag}>" for value in row)
parts.append(f"<tr>{cells}</tr>")
parts.append("</table>")
with open("output.html", "w", encoding="utf-8") as output:
output.write("\n".join(parts)).venv/bin/python task.pyLimits and output
- These data recipes use saved cell results, not recalculated formulas. They do not copy charts, styles, macros or workbook relationships. Keep the original file.
- This produces plain HTML table data, not an Excel page-layout reproduction.
Documentation: openpyxl tutorial and workbook loading options