Created
November 3, 2021 13:35
-
-
Save hminle/06b2dc804cfe8f881a3803047d9a4843 to your computer and use it in GitHub Desktop.
merge pdf files in a given path with password. Replace your <path> and <password> to use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PyPDF2 | |
from pathlib import Path | |
root = Path(r"<path>") | |
pdf_files = list(root.glob('*.pdf')) | |
writer = PyPDF2.PdfFileWriter() | |
for file in pdf_files: | |
with open(file, mode='rb') as f: | |
reader = PyPDF2.PdfFileReader(f) | |
if reader.isEncrypted: | |
reader.decrypt('<password>') | |
for pageNum in range(reader.getNumPages()): | |
writer.addPage(reader.getPage(pageNum)) | |
with open("merged_output.pdf", "wb") as out: | |
writer.write(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference:
https://stackoverflow.com/questions/44375872/pypdf2-returning-blank-pdf-after-copy
https://realpython.com/pdf-python/#how-to-merge-pdfs
https://caendkoelsch.wordpress.com/2019/05/10/merging-multiple-pdfs-into-a-single-pdf/
https://www.soudegesu.com/en/post/python/open-pdf-with-pypdf2/#success-example
https://pythonhosted.org/PyPDF2/