#Python rotate pdf page pdf
We also use the os module to create the directory where we want to split the PDF and then changed the working directory of our python code to that directory using the OS module’s chdir() function. Then we created a pdf reader object by using the PdfFileReader() method.
![python rotate pdf page python rotate pdf page](https://www.journaldev.com/wp-content/uploads/2019/10/PyPDF2-Python-PDF-toolkit.png)
Next, we declare some variable strings, namely path, split_name, split_dir, that contains the path to the pdf we want to split, the prefix for the name of the split files, and the directory we want to keep the split files, respectively. In the above code, we first imported the PyPDF2 and OS modules in our code using the python import statement. Print(f' The PDF file has been splitted in the directory ') # splitting the pdf into individual pages The following example shows how we can split a single pdf into multiple PDFs. We can also split a single PDF into multiple PDFs.
#Python rotate pdf page how to
In the previous topic, we have seen how to merge multiple PDFs into a single PDF. We will get a new PDF file created in the current working directory with the name merged.pdf on running the above code. We then used the write() method of pdf merger object to write the merged file to a PDF file with the name merged.pdf. We used the pdf merger’s append() method to append the two PDFs to the pdf merger object. Next, we created a PDF merger object using the PdfFieMerger() method of the PyPDF2 library. You can give any number of PDFs you want to merge on the list. Then we created a Python List that contains the name of two PDFs that we want to merge. We first imported the PyPDF2 module in our code by using the import statement of python. # writing the merged pdf in the output file The below code shows a practical demonstration of merging two PDFs into a single file. We can use python to merge two or more PDFs into a single file easily. There are situations to merge two or more PDFs into a single file. On running the above code, we will get a new file generated with the name new.pdf containing the first page of the sample pdf rotated in 90 degrees counter-clockwise direction. Finally, we close each of the open files by using the close() function of python. We then used the pdf writer object to add the rotated page to it and write that page to a new pdf file with the name new.pdf. Next, we use the method rotateCounterClockwise() with an argument 90 to rotate the pdf’s first page at 90 degrees in the anti-clockwise direction. After that, we use the getPage() pdf reader object’s method with an argument 0 to get the first page of the input PDF file. We created a pdf reader and writer objects by using the PdfFileReader and PdfFileWriter methods of the PyPDF2 module. Then we open the sample.pdf file for reading in binary mode by specifying the string “rb” in the argument. In the above code, we first imported the PyPDF2 library in our code using the import statement of python. # writing pdf data of the rotated page to the new file # opening a new file for writing in binary mode # adding the rotated page to the pdf_writer # rotating the first page to 90 degree anti-clockwiseįirst_page_rotate = first_page.rotateCounterClockwise(90) Pdf_reader = PyPDF2.PdfFileReader(pdfFileObj) On running the above code in python we will get the following output. In the last line of the code block, we used python’s close() function to close the PDF file.
![python rotate pdf page python rotate pdf page](https://i.stack.imgur.com/NHrob.png)
Then we use the print() method to display the data stored in the python dictionary returned by the getDocumentInfo() method. It also returns an object with a method named getDocumentInfo() that can be used to extract the metadata from the PDF file. Then we used the PdfFileReader function of the PyPDF2 library, which accepts the pdf file object as its argument and will read the pdf file. The open() function will return a file object, which we stored in the pdfFileObj variable. Here I specify the mode to be rb, which means that we will open the reading file in binary mode. While using the open() function of python, we need to specify the file mode to open the file. Then we open the sample.pdf file using the open() function of python. In the above code, we first imported the PyPDF2 library in our code using the import statement. Print("Total number of pages :", pdf.getNumPages())
![python rotate pdf page python rotate pdf page](https://1agenstvo.ru/800/600/https/print-prime.ru/uploads/posts/2016-03/html-academy-otlichnye-onlayn-kursy-po-html-i-css-verstka_4.jpeg)
# change the name sample.pdf to your pdf file