Last active
June 16, 2021 01:34
-
-
Save manojmohangit/30976aa24089a90e0a19c1d50cc7d8b0 to your computer and use it in GitHub Desktop.
Python Script to reorder the sequence of PDF 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
# Run the file - python reorder-pdf-file-python-script.py <input-filename> <output-filename> | |
# Reference from geeksforgeeks - https://www.geeksforgeeks.org/working-with-pdf-files-in-python/ | |
# Used Python Library - PyPDF2 and sys | |
import PyPDF2 | |
import sys | |
def PDFmerge(pdfs, output): | |
# creating pdf file merger object | |
pdfMerger = PyPDF2.PdfFileMerger() | |
# appending pdfs one by one | |
for pdf in pdfs: | |
with open(pdf, 'rb') as f: | |
pdfMerger.append(f) | |
# writing combined pdf to output pdf file | |
with open(output, 'wb') as f: | |
pdfMerger.write(f) | |
def PDFsplit(pdf): | |
# creating input pdf file object | |
pdfFileObj = open(pdf, 'rb') | |
# creating pdf reader object | |
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, False) | |
for i in range(pdfReader.numPages): | |
# creating pdf writer object for (i+1)th split | |
pdfWriter = PyPDF2.PdfFileWriter() | |
# output pdf file name | |
outputpdf = pdf.split('.pdf')[0] + str(i) + '.pdf' | |
# adding pages to pdf writer object | |
pdfWriter.addPage(pdfReader.getPage(i)) | |
# writing split pdf pages to pdf file | |
with open(outputpdf, "wb") as f: | |
pdfWriter.write(f) | |
# closing the input pdf file object | |
pdfFileObj.close() | |
def main(): | |
# pdf file to split | |
if len(sys.argv) < 3: | |
print("Please pass the file name in the argument") | |
return "" | |
pdf = str(sys.argv[1]) | |
# split page positions | |
# calling PDFsplit function to split pdf | |
PDFsplit(pdf) | |
# pdf files to merge | |
pdfFileObj = open(pdf, 'rb') | |
# creating pdf reader object | |
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, False) | |
pdfs = [] | |
for i in range(pdfReader.numPages): | |
pdfs.append(pdf.split('.pdf')[0] + str(pdfReader.numPages - 1 - i) + '.pdf') | |
# output pdf file name | |
output = str(sys.argv[2]) | |
# calling pdf merge function | |
PDFmerge(pdfs = pdfs, output = output) | |
if __name__ == "__main__": | |
# calling the main function | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment