Skip to content

Instantly share code, notes, and snippets.

@nyawach
Created October 3, 2019 05:21
Show Gist options
  • Save nyawach/d11e61716aa6ef6ee35e86346328f4a8 to your computer and use it in GitHub Desktop.
Save nyawach/d11e61716aa6ef6ee35e86346328f4a8 to your computer and use it in GitHub Desktop.
PDF結合する
import PyPDF2
from pathlib import Path
def merge_pdf(sources: list, filename: str):
"""PDFを結合する
- sources: もととなるソースファイル。配列の順番通りに結合する。
- filename: 出力するファイル名。
"""
merger = PyPDF2.PdfFileMerger()
for source in sources:
merger.append(str(source))
merger.write(filename)
merger.close()
if __name__ == "__main__":
path = Path("./")
sources = sorted(list(path.glob("*-A.pdf")))
merge_pdf(sources, './A-Result.pdf')
sources = sorted(list(path.glob("*-B.pdf")))
merge_pdf(sources, './B-Result.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment