I am trying to create a file converter application using streamlit in which i am trying to convert the input files into various file formats, I am stuck with the functionality of rearranging/reordering the pages in a pdf.
I would like if someone can provide the code with latest libraries for me to sort this out!
I have tried using PyPDF2 but the methods i tried are showing to be outdated.
Ravi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You should be able to reorder the pages in the pdf as follows:
from PyPDF2 import PdfWriter, PdfReader
output_pdf = PdfWriter()
#this is the order in which you want your pages to be in in the output
page_order = [2,3,0,1]
with open(r'input.pdf', 'rb') as readfile:
input_pdf = PdfReader(readfile)
for page in page_order:
output_pdf.add_page(input_pdf.pages[page])
with open(r'output.pdf', "wb") as writefile:
output_pdf.write(writefile)
None of the functions/imports here are currently depreciated, but it does need you to have an array that contains the order of pages that you want in your output file and it makes no checks to see if pages are missing or if you try to access a page that does not exsit
2