How to Convert Excel, Word, PowerPoint and PDF Files to Markdown With Python
Microsoft’s MarkItDown library converts Excel, Word, PowerPoint and PDF files to Markdown in three lines of Python: install it with the all extra, call convert() and read result.markdown. Markdown keeps headings and tables, which helps when you feed documents to ChatGPT or another language model.
Why Markdown
Markdown is close to plain text but keeps the structure of a document: headings, lists and tables. Language models such as ChatGPT read that structure well, so a document converted to Markdown gives better answers than raw extracted text, for example as knowledge for a custom GPT.
A spreadsheet pasted as plain text loses which value belongs to which column. As a Markdown table, every row and header stays intact, and the file is still readable for a person.
Install MarkItDown
MarkItDown is an open-source Python library from Microsoft. It reads PDF, Word, Excel, PowerPoint, HTML, CSV and ZIP files, and it can also turn a YouTube URL into Markdown. Since version 0.1, pip install markitdown installs only the core, so install the format support with it:
pip install "markitdown[all]"
Without the extra, converting a PDF fails with a MissingDependencyException. If you only need some formats, the library also has smaller extras such as pdf, docx, xlsx and pptx.
Convert a PDF
Create a MarkItDown object, pass the file path to convert, and the Markdown text is in result.markdown. The same three lines work for a .docx, .xlsx or .pptx file; MarkItDown picks the converter from the file type.
from markitdown import MarkItDown
md = MarkItDown(enable_plugins=False) # Set to True to enable plugins
result = md.convert("invoicesample.pdf")
print(result.markdown)
The script prints the text of the PDF to the terminal. To keep it, write result.markdown to a .md file. A scanned PDF has no text layer, so the result is empty; MarkItDown only reads scans through its optional Azure Document Intelligence integration.
Excel files
MarkItDown turns every sheet of a workbook into a level-2 heading with the sheet name, followed by the sheet’s data as a Markdown table. A workbook with a Sales sheet and a Weekly Report sheet becomes two sections, each with its own table.
The first row is used as the table header. A blank header cell comes out as Unnamed: 0, so give every column a name before you convert. Formulas, formatting and charts are not part of the output, only the values.
YouTube videos
Pass a YouTube URL to convert instead of a file path and MarkItDown returns the video’s title, metadata, description and, when the video has captions, the full transcript. The transcript needs the youtube-transcript-api package, which the all extra installs.
That makes it easy to give a language model the content of a tutorial, for example to turn a video into notes or questions.
Without code
In the video I converted the files with a small Streamlit app built on MarkItDown. That demo is no longer public; a free browser converter replaces it, and it handles one file at a time with no install.
Open the Office to Markdown converter, drop in an Excel, Word, PDF or PowerPoint file and copy or download the Markdown.
For whole folders or scanned files, my Windows app Office2MD converts on your own PC without uploading anything and reads scans with built-in OCR. For a structured alternative to Markdown, turning Excel data into JSON works well with ChatGPT.
