-
Notifications
You must be signed in to change notification settings - Fork 1.5k
pdf合并 #107
nanocm
started this conversation in
Show and tell
pdf合并
#107
-
from pathlib import Path from pypdf import PdfWriter book_paths = list(Path('.').glob('Book*')) book_paths = [path for path in book_paths if path.is_dir()] for path in book_paths: title = path.stem merger = PdfWriter() pdf_paths = list(path.glob('Book*.pdf')) flag = 1 try: pdf_paths.sort(key=lambda x: int(x.stem.split('_')[3])) except ValueError: pdf_paths.sort(key=lambda x: int(x.stem.split('_')[1][2:])) flag = 2 for pdf in pdf_paths: if flag == 1: bookmark = ' '.join(pdf.stem.split('_')[3:6]) else: tmp = pdf.stem.split('_') ch = tmp[1][2:] bookmark = f"{ch} {tmp[2]}" # merger.append(PdfReader(pdf), outline_item=bookmark) merger.append(pdf, outline_item=bookmark) with open(f'{title}.pdf', 'wb') as f: merger.write(f) merger.close()
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment