-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdoc_parse.py
More file actions
275 lines (218 loc) · 8.71 KB
/
doc_parse.py
File metadata and controls
275 lines (218 loc) · 8.71 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
#!/usr/bin/env python3
"""
Script to extract base64 inlined images from PDF or markdown files and replace them with file references.
The script:
1. For PDF files: Converts to markdown using docling CLI tool
2. Finds all base64 inlined images in the markdown file
3. Creates a subfolder named <documentName>_images
4. Saves images with increasing number names (1.png, 2.jpg, etc.)
5. Updates the markdown to reference these image files instead of base64 data
Requirements:
- docling must be installed for PDF conversion (pip install docling)
"""
import re
import base64
import os
import sys
import subprocess
from pathlib import Path
from typing import List, Tuple
def detect_image_format(base64_data: str) -> str:
"""
Detect image format from base64 data by checking the header.
Args:
base64_data: Base64 encoded image data
Returns:
File extension (png, jpg, gif, etc.)
"""
try:
# Decode just the first few bytes to check the signature
decoded_header = base64.b64decode(base64_data[:50])
# Check common image signatures
if decoded_header.startswith(b'\x89PNG\r\n\x1a\n'):
return 'png'
elif decoded_header.startswith(b'\xff\xd8\xff'):
return 'jpg'
elif decoded_header.startswith(b'GIF8'):
return 'gif'
elif decoded_header.startswith(b'RIFF') and b'WEBP' in decoded_header[:20]:
return 'webp'
elif decoded_header.startswith(b'BM'):
return 'bmp'
else:
# Default to png if we can't determine
return 'png'
except Exception:
# If we can't decode or detect, default to png
return 'png'
def convert_pdf_to_markdown(pdf_path: str) -> str:
"""
Convert a PDF file to markdown using the docling CLI tool.
Args:
pdf_path: Path to the PDF file
Returns:
Path to the generated markdown file
Raises:
RuntimeError: If docling is not installed or conversion fails
"""
# Check if docling is available
try:
subprocess.run(['docling', '--version'],
capture_output=True,
check=True)
except (subprocess.CalledProcessError, FileNotFoundError):
raise RuntimeError(
"docling is not installed or not in PATH. "
"Please install it first: pip install docling"
)
# Derive expected markdown filename
# Docling creates the .md file in the current working directory, not the PDF's directory
pdf_path_obj = Path(pdf_path)
pdf_basename = pdf_path_obj.stem # Get filename without extension
expected_md_path = Path.cwd() / f"{pdf_basename}.md"
print(f"Converting PDF to markdown using docling: {pdf_path}")
# Execute docling conversion
try:
result = subprocess.run(
['docling', pdf_path],
capture_output=True,
text=True,
check=True
)
print(f"Docling output: {result.stdout}")
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"Docling conversion failed with error:\n{e.stderr}"
)
# Verify the markdown file was created
if not expected_md_path.exists():
raise RuntimeError(
f"Expected markdown file not found: {expected_md_path}\n"
f"Docling may have failed silently or used a different output path."
)
print(f"Successfully converted to: {expected_md_path}")
return str(expected_md_path)
def extract_base64_images(markdown_content: str) -> List[Tuple[str, str, str, str]]:
"""
Extract base64 inlined images from markdown content.
Args:
markdown_content: The markdown file content
Returns:
List of tuples containing (full_match, alt_text, mime_type, base64_data)
"""
# Pattern to match markdown images with base64 data
# 
pattern = r'!\[([^\]]*)\]\(data:image/([^;]+);base64,([A-Za-z0-9+/=]+)\)'
matches = []
for match in re.finditer(pattern, markdown_content):
full_match = match.group(0)
alt_text = match.group(1)
mime_type = match.group(2)
base64_data = match.group(3)
matches.append((full_match, alt_text, mime_type, base64_data))
return matches
def process_markdown_file(file_path: str) -> bool:
"""
Process a markdown file to extract base64 images and update references.
Args:
file_path: Path to the markdown file
Returns:
True if successful, False otherwise
"""
try:
# Read the markdown file
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Extract base64 images
base64_images = extract_base64_images(content)
if not base64_images:
print(f"No base64 images found in {file_path}")
return True
print(f"Found {len(base64_images)} base64 images in {file_path}")
# Create images folder
file_path_obj = Path(file_path)
document_name = file_path_obj.stem
images_folder = file_path_obj.parent / f"{document_name}_images"
images_folder.mkdir(exist_ok=True)
print(f"Created images folder: {images_folder}")
# Process each image
updated_content = content
for i, (full_match, alt_text, mime_type, base64_data) in enumerate(base64_images, 1):
try:
# Decode base64 data
image_data = base64.b64decode(base64_data)
# Determine file extension
if mime_type in ['jpeg']:
extension = 'jpg'
else:
extension = mime_type.lower()
# Validate extension by checking actual image format
detected_format = detect_image_format(base64_data)
if detected_format != extension:
print(f"Warning: MIME type suggests {extension} but detected {detected_format}. Using detected format.")
extension = detected_format
# Create filename
filename = f"{i}.{extension}"
image_path = images_folder / filename
# Save image
with open(image_path, 'wb') as img_file:
img_file.write(image_data)
print(f"Saved image {i}: {filename} ({len(image_data)} bytes)")
# Create new markdown reference
relative_path = f"{document_name}_images/{filename}"
new_reference = f""
# Replace in content
updated_content = updated_content.replace(full_match, new_reference, 1)
except Exception as e:
print(f"Error processing image {i}: {e}")
continue
# Write updated markdown file
backup_path = f"{file_path}.backup"
os.rename(file_path, backup_path)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(updated_content)
print(f"Updated markdown file saved. Original backed up as: {backup_path}")
return True
except Exception as e:
print(f"Error processing file {file_path}: {e}")
return False
def main():
"""Main function to handle command line arguments and process files."""
if len(sys.argv) != 2:
print("Usage: python extract_images.py <file_path>")
print("Supported formats: PDF (.pdf) or Markdown (.md)")
print("Examples:")
print(" python extract_images.py document.pdf")
print(" python extract_images.py document.md")
sys.exit(1)
file_path = sys.argv[1]
if not os.path.exists(file_path):
print(f"Error: File {file_path} does not exist.")
sys.exit(1)
file_path_lower = file_path.lower()
# Handle PDF files - convert to markdown first
if file_path_lower.endswith('.pdf'):
print(f"Processing PDF file: {file_path}")
try:
md_file_path = convert_pdf_to_markdown(file_path)
print(f"Processing generated markdown file: {md_file_path}")
success = process_markdown_file(md_file_path)
except RuntimeError as e:
print(f"Error: {e}")
sys.exit(1)
# Handle markdown files directly
elif file_path_lower.endswith('.md'):
print(f"Processing markdown file: {file_path}")
success = process_markdown_file(file_path)
# Unsupported file type
else:
print(f"Error: Unsupported file type: {file_path}")
print("Please provide a PDF (.pdf) or Markdown (.md) file.")
sys.exit(1)
if success:
print("Processing completed successfully!")
else:
print("Processing failed!")
sys.exit(1)
if __name__ == "__main__":
main()