File size: 882 Bytes
77fbded |
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 |
import re
from pathlib import Path
from shutil import copy2
import pymupdf
def remove_images_from_markdown(markdown_text):
# remove <image> and  from markdown
markdown_text = re.sub(r"<img[^>]*>", "", markdown_text)
markdown_text = re.sub(r"!\[[^\]]*\]\([^)]*\)", "", markdown_text)
return markdown_text
def trim_pages(pdf_path, output_path, trim_pages=5):
doc = pymupdf.open(pdf_path)
parent_dir_name = Path(pdf_path).parent.name
output_file_path = Path(output_path) / f"{parent_dir_name}.pdf"
num_pages = len(doc)
if num_pages > trim_pages:
to_select = list(range(trim_pages))
doc.select(to_select)
doc.ez_save(output_file_path)
print("Trimmed pdf to with pages", to_select, "path", output_file_path)
else:
copy2(pdf_path, str(output_file_path))
return str(output_file_path)
|