Last active
October 27, 2020 02:09
-
-
Save flymin/58fd72cf3f628404e7a62cd4530db556 to your computer and use it in GitHub Desktop.
marge all pdf files and delete cover page in each file
This file contains hidden or 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
| from PyPDF2 import PdfFileWriter, PdfFileReader | |
| import os | |
| def merge_and_delete(files): | |
| output = PdfFileWriter() | |
| outpages = 0 | |
| for file in files: | |
| path = os.path.join("book", file) | |
| print(path) | |
| input1 = PdfFileReader(open(path, 'rb')) | |
| pages = input1.getNumPages() | |
| for i in range(pages): | |
| if i == 0: | |
| continue | |
| output.addPage(input1.getPage(i)) | |
| outpages += 1 | |
| output.addBookmark(title=file[:-4], pagenum=outpages-pages + 1) | |
| pages = output.getNumPages() | |
| outputStream = open("PyPDF2-output.pdf", "wb") | |
| output.write(outputStream) | |
| if __name__ == '__main__': | |
| files = sorted( | |
| [f for f in os.listdir("book") if f.endswith(".pdf")] | |
| ) | |
| merge_and_delete(files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment