-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_to_markdown.py
More file actions
349 lines (300 loc) · 12 KB
/
Copy pathpdf_to_markdown.py
File metadata and controls
349 lines (300 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
"""
PDF to Markdown Converter
Extracts text directly from PDF using PyMuPDF for accuracy and speed.
Extracts embedded figures as image files and places them inline with
the surrounding text based on their position on the page.
Optionally uses Claude vision API to describe figures.
Requirements:
pip install pymupdf
pip install anthropic (optional, for figure descriptions)
"""
import argparse
import base64
import hashlib
import os
import sys
import time
from pathlib import Path
import fitz # PyMuPDF
def extract_page_content(
doc: fitz.Document, page_num: int, figures_dir: Path, page_label: str
) -> list[dict]:
"""Extract interleaved text and image blocks from a page, sorted by Y position.
Returns a list of dicts, each either:
{"type": "text", "y": float, "content": str}
{"type": "image", "y": float, "filename": str, "width": int, "height": int}
"""
page = doc.load_page(page_num)
blocks = page.get_text("dict")["blocks"]
result = []
img_counter = 0
seen_image_hashes = set()
for block in blocks:
y_pos = block["bbox"][1]
block_type = block.get("type", 0)
if block_type == 0: # text block
lines_text = []
for line in block.get("lines", []):
span_texts = []
for span in line.get("spans", []):
t = span.get("text", "")
if t:
span_texts.append(t)
line_str = "".join(span_texts).strip()
if line_str:
lines_text.append(line_str)
content = "\n".join(lines_text)
if content.strip():
result.append({"type": "text", "y": y_pos, "content": content})
elif block_type == 1: # image block
# Extract the image via xref from the block
img_w = block["bbox"][2] - block["bbox"][0]
img_h = block["bbox"][3] - block["bbox"][1]
# Skip tiny images
if img_w < 30 or img_h < 30:
continue
# Try to get the image data from the block
# PyMuPDF dict blocks include image data for type=1
img_bytes = block.get("image")
ext = block.get("ext", "png")
if not ext:
ext = "png"
if img_bytes:
# Deduplicate by content hash
img_hash = hashlib.md5(img_bytes).hexdigest()
if img_hash in seen_image_hashes:
continue
seen_image_hashes.add(img_hash)
img_counter += 1
filename = f"{page_label}_fig{img_counter}.{ext}"
filepath = figures_dir / filename
filepath.write_bytes(img_bytes)
result.append({
"type": "image",
"y": y_pos,
"filename": filename,
"width": int(img_w),
"height": int(img_h),
})
# Also extract via get_images for higher quality versions
# and any images not captured by dict blocks
page_images = page.get_images(full=True)
seen_xrefs = set()
for img_info in page_images:
xref = img_info[0]
if xref in seen_xrefs:
continue
seen_xrefs.add(xref)
try:
base_image = doc.extract_image(xref)
except Exception:
continue
w = base_image.get("width", 0)
h = base_image.get("height", 0)
if w < 50 or h < 50:
continue
img_data = base_image["image"]
# Check if we already have this image (by matching file content size)
already_saved = False
for item in result:
if item["type"] != "image":
continue
filepath = figures_dir / item["filename"]
if not filepath.exists():
continue
existing_size = filepath.stat().st_size
# Same data or higher quality replacement
if existing_size == len(img_data):
already_saved = True
break
# Similar display dimensions = same image, keep higher quality
if abs(item.get("width", 0) - w) < 50 and abs(item.get("height", 0) - h) < 50:
if len(img_data) > existing_size:
filepath.write_bytes(img_data)
already_saved = True
break
if not already_saved:
img_counter += 1
ext = base_image.get("ext", "png")
filename = f"{page_label}_fig{img_counter}.{ext}"
filepath = figures_dir / filename
filepath.write_bytes(img_data)
result.append({
"type": "image",
"y": 9999,
"filename": filename,
"width": w,
"height": h,
})
# Sort by Y position
result.sort(key=lambda b: b["y"])
return result
def describe_figure_with_llm(client, image_path: Path, model: str) -> str:
"""Use Claude vision to describe a figure."""
image_bytes = image_path.read_bytes()
ext = image_path.suffix.lower().lstrip(".")
media_type = {
"jpg": "image/jpeg", "jpeg": "image/jpeg",
"png": "image/png", "gif": "image/gif", "webp": "image/webp",
}.get(ext, "image/png")
b64 = base64.standard_b64encode(image_bytes).decode("utf-8")
for attempt in range(3):
try:
response = client.messages.create(
model=model,
max_tokens=1024,
messages=[{
"role": "user",
"content": [
{
"type": "image",
"source": {"type": "base64", "media_type": media_type, "data": b64},
},
{
"type": "text",
"text": (
"Describe this chart/figure in 1-2 sentences. "
"Focus on what it shows. Respond in the same language as "
"any text in the image. Output ONLY the description."
),
},
],
}],
)
return response.content[0].text.strip()
except Exception:
if attempt < 2:
time.sleep(2 ** attempt * 3)
else:
return "图表"
return "图表"
def convert_pdf(
pdf_path: str,
output_path: str | None = None,
figures_dir: str | None = None,
describe_figures: bool = False,
model: str = "claude-sonnet-4-20250514",
start_page: int = 1,
end_page: int | None = None,
) -> str:
"""Convert a PDF file to Markdown with figures inline.
Args:
pdf_path: Path to the PDF file.
output_path: Output .md file path.
figures_dir: Directory to save extracted figures.
describe_figures: Use Claude API to describe figures.
model: Claude model for figure descriptions.
start_page: First page (1-based).
end_page: Last page (1-based, inclusive).
Returns:
Path to the output Markdown file.
"""
pdf_path = Path(pdf_path)
if not pdf_path.exists():
raise FileNotFoundError(f"PDF not found: {pdf_path}")
if output_path is None:
output_path = pdf_path.with_suffix(".md")
else:
output_path = Path(output_path)
if figures_dir is None:
figures_dir = pdf_path.stem + "_figures"
figures_dir = Path(figures_dir)
figures_dir.mkdir(parents=True, exist_ok=True)
# Set up LLM client if needed
client = None
if describe_figures:
try:
import anthropic
api_key = os.environ.get("SOLVIFY_ANTHROPIC_KEY") or os.environ.get("ANTHROPIC_API_KEY")
if api_key:
client = anthropic.Anthropic(api_key=api_key)
else:
print("Warning: No API key found. Skipping figure descriptions.", file=sys.stderr)
describe_figures = False
except ImportError:
print("Warning: anthropic not installed. Skipping figure descriptions.", file=sys.stderr)
describe_figures = False
doc = fitz.open(str(pdf_path))
total_pages = doc.page_count
start_idx = max(0, start_page - 1)
end_idx = min(total_pages, end_page) if end_page else total_pages
print(f"PDF: {pdf_path} ({total_pages} pages)")
print(f"Processing pages {start_idx + 1}-{end_idx}")
print(f"Output: {output_path}")
print(f"Figures: {figures_dir}")
if describe_figures:
print(f"Figure descriptions: ON (model: {model})")
print()
total_figures = 0
with open(output_path, "w", encoding="utf-8") as f:
f.write(f"# {pdf_path.stem}\n\n")
for page_num in range(start_idx, end_idx):
page_display = page_num + 1
page_label = f"p{page_display:03d}"
# Extract interleaved text and images
blocks = extract_page_content(doc, page_num, figures_dir, page_label)
fig_count = sum(1 for b in blocks if b["type"] == "image")
total_figures += fig_count
fig_indicator = f" [{fig_count} fig]" if fig_count else ""
print(f" Page {page_display}/{end_idx}{fig_indicator}", flush=True)
# Write page marker
f.write(f"<!-- Page {page_display} -->\n\n")
# Write blocks in order (text and images interleaved)
for block in blocks:
if block["type"] == "text":
f.write(block["content"])
f.write("\n\n")
elif block["type"] == "image":
fig_path = figures_dir / block["filename"]
alt_text = f"Figure from page {page_display}"
if describe_figures and client:
alt_text = describe_figure_with_llm(client, fig_path, model)
f.write(f"\n\n")
f.write("---\n\n")
f.flush()
doc.close()
print(f"\nDone! {end_idx - start_idx} pages processed, {total_figures} figures extracted.")
print(f"Output: {output_path}")
print(f"Figures directory: {figures_dir}")
return str(output_path)
def main():
parser = argparse.ArgumentParser(
description="Convert PDF to Markdown with inline figure extraction",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s document.pdf # basic extraction
%(prog)s document.pdf -o output.md # custom output path
%(prog)s document.pdf --start 10 --end 20 # page range
%(prog)s document.pdf --describe-figures # use LLM for figure captions
""",
)
parser.add_argument("pdf_file", help="Path to the PDF file")
parser.add_argument("-o", "--output", help="Output Markdown file path")
parser.add_argument("--figures-dir", help="Directory to save extracted figures")
parser.add_argument(
"--describe-figures", action="store_true",
help="Use Claude API to generate figure descriptions (requires API key)",
)
parser.add_argument(
"--model", default="claude-sonnet-4-20250514",
help="Claude model for figure descriptions (default: claude-sonnet-4-20250514)",
)
parser.add_argument("--start", type=int, default=1, help="Start page (1-based, default: 1)")
parser.add_argument("--end", type=int, default=None, help="End page (1-based, inclusive)")
args = parser.parse_args()
if not os.path.exists(args.pdf_file):
print(f"Error: File not found: {args.pdf_file}", file=sys.stderr)
sys.exit(1)
convert_pdf(
pdf_path=args.pdf_file,
output_path=args.output,
figures_dir=args.figures_dir,
describe_figures=args.describe_figures,
model=args.model,
start_page=args.start,
end_page=args.end,
)
if __name__ == "__main__":
main()