Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

pdf合并 #107

nanocm started this conversation in Show and tell
Oct 20, 2023 · 0 comments
Discussion options

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()
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /