chore: publish from main

This commit is contained in:
github-actions[bot]
2026-07-15 23:07:21 +00:00
parent 921c4cc181
commit 6a843b5c57
29 changed files with 3241 additions and 0 deletions
@@ -0,0 +1,130 @@
---
name: convert-pdf-to-md
description: 'Converts PDF (.pdf) documents into Markdown so their contents can be accurately analyzed, summarized, searched, or extracted from. Use this skill whenever the user shares, references, or asks about a .pdf file — even if they don''t say "convert" or "markdown" explicitly. This includes requests to "read", "summarize", "review", "extract data from", "compare", or "analyze" a PDF report, paper, invoice, form, contract, or scanned document. Always run the bundled conversion script to produce Markdown first; do not attempt to parse PDF content directly or write ad-hoc extraction code. Also use this skill for batch requests involving a whole folder of PDF documents. IMPORTANT: When the user references a folder or set of documents containing multiple file types (.pdf, .docx, .xlsx), invoke ALL three sibling skills — convert-pdf-to-md, convert-word-to-md, and convert-excel-to-md — so no file type is silently skipped.'
---
# Convert PDF to Markdown
## When to use this skill
Trigger this skill any time there is a `.pdf` file that needs to be
understood or processed — for example, a user attaches a PDF and asks
questions about it, wants a summary, wants specific data or tables pulled
out, or wants multiple PDFs in a folder processed together. PDF is a
layout/print format, not reliably readable as plain text, so always convert
it to Markdown first using the script in this skill rather than trying to
open or parse the file directly.
This skill only supports `.pdf` — that's MarkItDown's only PDF-family
format, so there's no legacy format to worry about here (unlike Word's
`.doc` or Excel's `.xls`).
**Mixed file types:** When the user references a folder or set of documents
containing multiple supported file types (`.pdf`, `.docx`, `.xlsx`), this
skill handles only `.pdf` files. The agent MUST also invoke the sibling
skills in parallel:
- `convert-word-to-md` for any `.docx` files
- `convert-excel-to-md` for any `.xlsx` files
Never process a folder and silently skip a supported file type. All three
skills must be invoked together when mixed types are present.
## Setup (once per environment)
Before the first conversion in a given environment, follow
[`references/setup.md`](references/setup.md) step by step to ensure Python,
pip, `markitdown`, and `pymupdf` (for image extraction) are installed. Do
this proactively rather than guessing whether the environment is ready — the
script itself will also fail with a clear pointer back to that file if a
dependency turns out to be missing, so it's safe to just try the conversion
first if you're reasonably confident setup was already done.
## Usage
The conversion script lives at `scripts/convert_pdf_to_md.py`.
**Output structure:** MarkItDown's PDF converter extracts text and tables
only — it has no concept of embedded images at all. This script separately
extracts real embedded images via PyMuPDF and writes a self-contained folder
per document:
```
<name>/
img/
page001_img001.<ext>
page002_img001.<ext>
...
<name>.md
```
Because MarkItDown's PDF text does not preserve reliable per-page markers,
there's no safe way to know exactly where inline an image belongs. Rather
than risk misplacing images next to the wrong paragraph, the script appends
a `## Extracted Images` section at the end of the Markdown, with a
`### Page N` subheading per page that has images — read this section
separately from the main body text. If the document has no embedded images,
no `img/` folder or `Extracted Images` section is created.
**Single file:**
```powershell
python scripts\convert_pdf_to_md.py "C:\path\to\document.pdf"
```
This creates a `document\` folder next to the source file (containing
`document.md` and, if present, `document\img\`). To control the destination
folder explicitly:
```powershell
python scripts\convert_pdf_to_md.py "C:\path\to\document.pdf" -o "C:\path\to\output_folder"
```
**A folder of PDFs (batch mode):**
```powershell
python scripts\convert_pdf_to_md.py "C:\path\to\folder"
```
Add `--recursive` to also include subfolders:
```powershell
python scripts\convert_pdf_to_md.py "C:\path\to\folder" --recursive
```
Each `.pdf` found gets its own `<name>\` output folder next to it by
default. Pass `-o "C:\path\to\output_parent"` to collect all the generated
`<name>\` folders under a separate parent directory instead (subfolder
structure is preserved when combined with `--recursive`).
After conversion, read the resulting `.md` file(s) to perform the actual
analysis the user asked for — the script's job is only to produce accurate
Markdown (and images), not to interpret the content.
## Deciding where output goes
**Default — always output next to the source file.** The `<name>/` folder
is created in the same directory as the source `.pdf`. This is the required
default for every case. Do NOT override it unless the user explicitly asks
for a different location.
**Only use `-o` when** the user explicitly provides an output path (e.g.,
"save the output to `C:\output`", "put the results in `D:\work`"). Do NOT
pass `-o` based on the agent's current working directory, the session state
folder, or any implied location.
**If the source file path cannot be fully resolved** — for example, the
user provides only a filename with no directory, or the path is ambiguous —
use `ask_user` to confirm the full absolute path before running the
conversion. Never guess or assume the directory.
## Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError: No module named 'markitdown'` or `'fitz'` / exit code 2 | MarkItDown or PyMuPDF not installed | Follow `references/setup.md` |
| `ERROR: Unsupported file type '...'` / exit code 3 | Not a `.pdf` file | Ask the user for the correct file, or if it's `.doc`/`.docx`/`.xlsx`, use the matching sibling skill instead |
| `ERROR: Input path not found` / exit code 3 | Wrong path, or file moved | Confirm the correct path with the user |
| `FAILED <file> -> ...` in batch output | That specific file is corrupt, password-protected, or otherwise unreadable | Report which file(s) failed; other files in the batch still succeed |
| `NOTE: skipped N non-.pdf file(s)` | Folder contains non-PDF files | Expected — those files are intentionally ignored |
| Markdown body is empty or near-empty despite images being extracted | The PDF is scanned/image-only with no embedded text layer; MarkItDown does not perform OCR | Tell the user OCR isn't supported — the extracted page images are still available for them to view |
| Images appear in an appendix instead of inline with the text | Deliberate limitation — MarkItDown's PDF text has no reliable per-page markers to place images inline | Expected behavior; cross-reference the `### Page N` heading with the surrounding text context if needed |
@@ -0,0 +1,71 @@
# Environment Setup for convert-pdf-to-md
Follow these steps exactly, in order, before running `scripts/convert_pdf_to_md.py`
for the first time in a given environment. Don't skip steps or improvise
alternatives — they're written to be deterministic and safe to re-run.
## 1. Check Python is available (3.10+)
```powershell
python --version
```
- If this fails (command not found), install Python 3.10 or newer:
- Windows: `winget install --id Python.Python.3.12 -e`
- macOS: `brew install python@3.12`
- Linux (Debian/Ubuntu): `sudo apt-get update && sudo apt-get install -y python3 python3-pip python-is-python3`
- If the reported version is older than 3.10, install a newer Python using
the same command above (MarkItDown requires 3.10+).
## 2. Check pip is available
```powershell
python -m pip --version
```
- If this fails, bootstrap pip:
```powershell
python -m ensurepip --upgrade
```
## 3. Install MarkItDown with PDF support, plus PyMuPDF for image extraction
Use the `scripts/requirements.txt` file bundled with this skill to install pinned,
known-good versions of the dependencies:
```powershell
python -m pip install -r scripts/requirements.txt
```
This pulls in `markitdown[pdf]` and `pymupdf>=1.24.0`. PyMuPDF (imported as `fitz`)
is required separately because MarkItDown's PDF
converter only extracts text and tables — it has no support for embedded
images at all, so this skill's script extracts them itself.
## 4. Verify the install
```powershell
python -c "from markitdown import MarkItDown; import fitz; print('markitdown + pymupdf OK')"
```
Expect to see `markitdown + pymupdf OK` printed with no errors. If you see a
`ModuleNotFoundError`, repeat step 3 — pip may be installing into a
different Python environment than the one being invoked (check
`python -m pip --version` shows the same path as `python --version`'s
interpreter).
## Notes
- This setup only needs to be done once per environment/virtual environment,
not once per conversion.
- `convert_pdf_to_md.py` itself also checks for `markitdown` and `fitz` at
startup and prints a pointer back to this file if either is missing, so
re-running setup is safe and idempotent.
- Only `.pdf` is supported by this skill — it's MarkItDown's only PDF-family
format, so there's no legacy-format equivalent to worry about (unlike
Word's `.doc` or Excel's `.xls`).
- Scanned/image-only PDFs (no embedded text layer) will produce little or
no text from MarkItDown, since it does not perform OCR. The images
themselves will still be extracted and appended, but the text body may be
empty or near-empty in that case — mention this to the user if it happens.
@@ -0,0 +1,321 @@
#!/usr/bin/env python3
"""Convert PDF documents to Markdown using Microsoft's MarkItDown, with
embedded images extracted to real files via PyMuPDF (MarkItDown's PDF
converter only extracts text/tables -- it does not detect or emit anything
for embedded images at all).
Usage:
python convert_pdf_to_md.py <input> [-o OUTPUT] [--recursive]
<input> may be either:
- a path to a single .pdf file, or
- a path to a directory (batch mode: every .pdf file directly inside it
is converted; pass --recursive to also descend into subdirectories).
Output:
For each source .pdf (named "<name>.pdf"), a folder is created containing
the Markdown and its images, in this layout:
<name>/
img/
page001_img001.<ext>
page001_img002.<ext>
page002_img001.<ext>
...
<name>.md
IMPORTANT: MarkItDown's PDF text extraction does not preserve reliable
per-page markers in the returned Markdown (pages are simply joined
together, or in some cases returned as a single unmarked block of text).
That means there is no safe way to know exactly where, inline, an image
should go. Rather than guess and risk misplacing an image next to the
wrong paragraph, this script appends a clearly labeled "## Extracted
Images" section at the end of the Markdown, with a "### Page N"
subheading per page that contains images. This is a deliberate, honest
tradeoff -- read the images section separately from the main body text.
- Single file mode: the "<name>/" folder is created next to the source
file, or at -o/--output (treated as the exact destination folder) if
given.
- Batch/directory mode: a "<name>/" folder is created next to each source
file, or under -o/--output (treated as a parent directory, created if
missing) if given, preserving relative subfolder structure when
--recursive is used.
- If a document has no embedded images, no "img/" folder or "Extracted
Images" section is created.
Exit codes:
0 - all requested conversions succeeded
1 - one or more conversions failed (partial success in batch mode)
2 - a required dependency ("markitdown" or "pymupdf") is not installed
3 - invalid input (path not found, or single-file input is not .pdf)
"""
import argparse
import sys
import hashlib
import shutil
from pathlib import Path
EXIT_OK = 0
EXIT_CONVERSION_FAILED = 1
EXIT_MISSING_DEPENDENCY = 2
EXIT_INVALID_INPUT = 3
def _import_markitdown():
"""Import MarkItDown, failing with a clear, actionable message if absent."""
try:
from markitdown import MarkItDown
return MarkItDown
except ImportError:
print(
"ERROR: The 'markitdown' package is not installed.\n"
"See references/setup.md for this skill, or run:\n"
' pip install "markitdown[pdf]"',
file=sys.stderr,
)
sys.exit(EXIT_MISSING_DEPENDENCY)
def _import_fitz():
"""Import PyMuPDF (module name 'fitz'), failing with a clear message if absent."""
try:
import fitz
import hashlib
return fitz
except ImportError:
print(
"ERROR: The 'pymupdf' package is not installed (needed for image "
"extraction).\nSee references/setup.md for this skill, or run:\n"
" pip install pymupdf",
file=sys.stderr,
)
sys.exit(EXIT_MISSING_DEPENDENCY)
def extract_images(fitz, pdf_path: Path, img_dir: Path):
"""Extract embedded images from pdf_path, grouped by 1-based page number.
Returns {page_num: [filename, ...]} in per-page image order. Files are
named 'page{P:03d}_img{N:03d}.<ext>'. Corrupt/unreadable images are
skipped with a warning rather than aborting the whole conversion.
Two sources are combined and deduplicated:
1. Image XObjects via page.get_images(full=True) -- covers most embedded
images in modern PDFs.
2. Inline image blocks via page.get_text("dict") -- covers images stored
directly in the page content stream, which get_images() misses entirely.
Deduplication is by image bytes hash so the same raster is never written twice
on the same page regardless of which source reported it."""
written_by_page = {}
try:
doc = fitz.open(str(pdf_path))
except Exception as exc: # noqa: BLE001
print(f"WARNING: could not open {pdf_path} for image extraction: {exc}", file=sys.stderr)
return written_by_page
try:
for page_index in range(len(doc)):
page = doc[page_index]
page_label = page_index + 1
seen_hashes: set = set()
raw_images: list[tuple[bytes, str]] = [] # (image_bytes, ext)
# --- Source 1: XObject images ---
try:
xobjects = page.get_images(full=True)
except Exception as exc: # noqa: BLE001
print(
f"WARNING: failed to enumerate XObject images on page {page_label} "
f"of {pdf_path}: {exc}",
file=sys.stderr,
)
xobjects = []
for img in xobjects:
xref = img[0]
try:
base_image = doc.extract_image(xref)
except Exception as exc: # noqa: BLE001
print(
f"WARNING: failed to extract XObject image xref={xref} on page "
f"{page_label} of {pdf_path}: {exc}",
file=sys.stderr,
)
continue
img_bytes = base_image.get("image") or b""
if not img_bytes:
continue
ext = (base_image.get("ext") or "png").lower()
raw_images.append((img_bytes, ext))
# --- Source 2: Inline images via get_text("dict") ---
try:
blocks = page.get_text("dict", flags=fitz.TEXT_PRESERVE_IMAGES).get("blocks", [])
except Exception as exc: # noqa: BLE001
print(
f"WARNING: failed to extract text/image dict on page {page_label} "
f"of {pdf_path}: {exc}",
file=sys.stderr,
)
blocks = []
for block in blocks:
# Image blocks have type == 1
if block.get("type") != 1:
continue
img_bytes = block.get("image") or b""
if not img_bytes:
continue
# Derive extension from the block's "ext" key (fitz sets this)
ext = (block.get("ext") or "png").lower()
raw_images.append((img_bytes, ext))
# --- Write deduplicated images ---
page_files = []
img_idx = 1
for img_bytes, ext in raw_images:
h = hashlib.sha256(img_bytes).digest()
if h in seen_hashes:
continue
seen_hashes.add(h)
out_name = f"page{page_label:03d}_img{img_idx:03d}.{ext}"
img_dir.mkdir(parents=True, exist_ok=True)
(img_dir / out_name).write_bytes(img_bytes)
page_files.append(out_name)
img_idx += 1
if page_files:
written_by_page[page_label] = page_files
finally:
doc.close()
return written_by_page
def build_image_appendix(written_by_page) -> str:
"""Build the '## Extracted Images' appendix text. Returns "" if empty."""
if not written_by_page:
return ""
lines = ["", "## Extracted Images", ""]
for page_num in sorted(written_by_page):
lines.append(f"### Page {page_num}")
lines.append("")
for name in written_by_page[page_num]:
lines.append(f"![{name}](img/{name})")
lines.append("")
return "\n".join(lines).rstrip() + "\n"
def convert_one(md, fitz, source: Path, dest_dir: Path) -> bool:
"""Convert a single .pdf file to a '<name>/' folder containing the
Markdown file and an 'img/' folder of extracted images. Returns True on
success."""
try:
result = md.convert(str(source))
except Exception as exc: # noqa: BLE001 - surface any conversion error
print(f"FAILED {source} -> {exc}", file=sys.stderr)
return False
try:
if dest_dir.exists():
shutil.rmtree(dest_dir)
dest_dir.mkdir(parents=True, exist_ok=True)
written_by_page = extract_images(fitz, source, dest_dir / "img")
appendix = build_image_appendix(written_by_page)
text = result.text_content.rstrip("\n")
full_text = f"{text}\n{appendix}" if appendix else f"{text}\n"
md_path = dest_dir / f"{source.stem}.md"
md_path.write_text(full_text, encoding="utf-8")
except OSError as exc:
print(f"FAILED {source} -> could not write output in {dest_dir}: {exc}", file=sys.stderr)
return False
img_count = sum(len(v) for v in written_by_page.values())
img_note = f", {img_count} image(s)" if img_count else ""
print(f"OK {source} -> {md_path}{img_note}")
return True
def find_pdf_files(root: Path, recursive: bool):
"""Return (pdf_files, skipped_count) for files directly/recursively under root."""
pattern_iter = root.rglob("*") if recursive else root.iterdir()
pdf_files = []
skipped = 0
for entry in pattern_iter:
if entry.is_dir():
continue
if entry.suffix.lower() == ".pdf":
pdf_files.append(entry)
else:
skipped += 1
return sorted(pdf_files), skipped
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("input", help="Path to a .pdf file or a directory of .pdf files")
parser.add_argument(
"-o", "--output",
help=(
"Destination folder for the '<name>/' output (single-file mode), "
"or parent directory under which each '<name>/' output folder is "
"created (batch mode)"
),
)
parser.add_argument(
"--recursive", action="store_true",
help="When input is a directory, also search subdirectories",
)
args = parser.parse_args()
#MarkItDown = _import_markitdown()
#fitz = _import_fitz()
#md = MarkItDown()
source = Path(args.input)
if not source.exists():
print(f"ERROR: Input path not found: {source}", file=sys.stderr)
return EXIT_INVALID_INPUT
if source.is_file() and source.suffix.lower() != ".pdf":
print(
f"ERROR: Unsupported file type '{source.suffix}'. "
"This skill only converts .pdf files.",
file=sys.stderr,
)
return EXIT_INVALID_INPUT
MarkItDown = _import_markitdown()
fitz = _import_fitz()
md = MarkItDown()
if source.is_file():
dest_dir = Path(args.output) if args.output else source.parent / source.stem
return EXIT_OK if convert_one(md, fitz, source, dest_dir) else EXIT_CONVERSION_FAILED
# Directory / batch mode
pdf_files, skipped = find_pdf_files(source, args.recursive)
if skipped:
print(f"NOTE: skipped {skipped} non-.pdf file(s) in {source}")
if not pdf_files:
print(f"ERROR: No .pdf files found under {source}", file=sys.stderr)
return EXIT_INVALID_INPUT
out_dir = Path(args.output) if args.output else None
success_count = 0
for pdf_path in pdf_files:
if out_dir is not None:
rel = pdf_path.relative_to(source)
dest_dir = out_dir / rel.parent / pdf_path.stem
else:
dest_dir = pdf_path.parent / pdf_path.stem
if convert_one(md, fitz, pdf_path, dest_dir):
success_count += 1
total = len(pdf_files)
print(f"\nConverted {success_count}/{total} file(s).")
return EXIT_OK if success_count == total else EXIT_CONVERSION_FAILED
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,2 @@
markitdown[pdf]>=0.1.0
pymupdf>=1.24.0