Convert Excel to Markdown with Python
Create a Markdown table with escaped HTML, inline Markdown and visible line breaks.
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()
def cell(value):
text = escape("" if value is None else str(value), quote=False).replace("\\", "\\\\")
for character in "*_{}[]()#+-.!|~" + chr(96):
text = text.replace(character, "\\" + character)
return text.replace("\r\n", "\n").replace("\r", "\n").replace("\n", "<br>")
if not rows:
raise ValueError("The worksheet is empty")
lines = ["| " + " | ".join(map(cell, rows[0])) + " |",
"| " + " | ".join("---" for _ in rows[0]) + " |"]
lines += ["| " + " | ".join(map(cell, row)) + " |" for row in rows[1:]]
with open("output.md", "w", encoding="utf-8") as output:
output.write("\n".join(lines) + "\n").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.
- The first row is the header. Markdown renderers vary, particularly in their support for HTML line breaks. This recipe holds the selected sheet in memory.
Documentation: openpyxl tutorial and workbook loading options