Replies: 2 comments
-
|
Page numbers are not accessible through markitdown currently, and this is a fundamental DOCX format limitation rather than a markitdown gap. Why DOCX does not store fixed page numbers: Workarounds for traceability: Option 1 — Convert to PDF first, then extract with page metadata # Use LibreOffice headless to produce a PDF, then pdfminer for page-aware extraction
import subprocess, pdfminer.high_level
subprocess.run(["soffice", "--headless", "--convert-to", "pdf", "doc.docx"])
pages = list(pdfminer.high_level.extract_pages("doc.pdf"))
for page_num, page in enumerate(pages, start=1):
# extract text per page
print(f"Page {page_num}: ...")Option 2 — Use from docx import Document
from docx.oxml.ns import qn
doc = Document("doc.docx")
page = 1
for para in doc.paragraphs:
# Check for explicit page break in runs
for run in para.runs:
for br in run._r.findall(qn("w:br")):
if br.get(qn("w:type")) == "page":
page += 1
text = para.text
if text:
print(f"[Page ~{page}] {text[:80]}")This catches manually-inserted page breaks but not soft/automatic ones. Option 3 — Embed page markers in the source document Future support for page metadata in markitdown would likely require an optional rendering pass (e.g., via LibreOffice UNO) — worth opening as a feature request if this is a common need for your use case. |
Beta Was this translation helpful? Give feedback.
-
|
import subprocess Step 1: Convert DOCX → PDFsubprocess.run([ Step 2: Read PDF page by pagefrom pdfminer.high_level import extract_pages pages = list(extract_pages("doc.pdf")) for i, page in enumerate(pages, 1): ✔ Accurate page mapping 🥈 Lightweight method (no conversion) doc = Document("doc.docx") for para in doc.paragraphs: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using markitdown to edit word.
But when I pull the text I don't know which page it belongs to, which is important to have traceability when I generate the chunks from that word.
Is there a way to get it with the current version?
If not, do you plan to do it in some future versions? because I see it interesting and useful.
Beta Was this translation helpful? Give feedback.
All reactions