![]() |
VOOZH | about |
All of you must be familiar with what PDFs are. In fact, they are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system.
Invented by Adobe, PDF is now an open standard maintained by the International Organization for Standardization (ISO). PDFs can contain links and buttons, form fields, audio, video, and business logic.
In this article, we will learn, how we can do various operations like:
Installation: Using simple python scripts!
We will be using a third-party module, pypdf.
pypdf is a python library built as a PDF toolkit. It is capable of:
To install pypdf, run the following command from the command line:
pip install pypdfThis module name is case-sensitive, so make sure the y is lowercase and everything else is uppercase. All the code and PDF files used in this tutorial/article are available here.
1. Extracting text from PDF file
The output of the above program looks like this:
20
PythonBasics
S.R.Doty
August27,2008
Contents
1Preliminaries
4
1.1WhatisPython?...................................
..4
1.2Installationanddocumentation....................
.........4 [and some more lines...]
Let us try to understand the above code in chunks:
reader = PdfReader('example.pdf')
print(len(reader.pages))
pageObj = reader.pages[0]
print(pageObj.extract_text())
Note: While PDF files are great for laying out text in a way that’s easy for people to print and read, they’re not straightforward for software to parse into plaintext. As such, pypdf might make mistakes when extracting text from a PDF and may even be unable to open some PDFs at all. It isn’t much you can do about this, unfortunately. pypdf may simply be unable to work with some of your particular PDF files.
2. Rotating PDF pages
Here, you can see how the first page of rotated_example.pdf looks like ( right image) after rotation:
Some important points related to the above code:
writer = PdfWriter()
for page in range(len(pdfReader.pages)):
pageObj = pdfReader.pages[page]
pageObj.rotate(rotation)
writer.add_page(pageObj)
newFile = open(newFileName, 'wb')
writer.write(newFile)
newFile.close()
3. Merging PDF files
The output of the above program is a combined PDF, combined_example.pdf, obtained by merging example.pdf and rotated_example.pdf.
pdfWriter = PdfWriter()# appending pdfs one by one
for pdf in pdfs:
pdfWriter.append(pdf)
# writing combined pdf to output pdf file
with open(output, 'wb') as f:
pdfWriter.write(f)
4. Splitting PDF file
Output will be three new PDF files with split 1 (page 0,1), split 2(page 2,3), split 3(page 4-end).
No new function or class has been used in the above python program. Using simple logic and iterations, we created the splits of passed PDF according to the passed list splits.
5. Adding watermark to PDF pages
Here is how the first page of original (left) and watermarked (right) PDF file looks like:
wmpageObj = add_watermark(mywatermark, pdfReader.pages[page])
reader = PdfReader(wmFile)
pageObj.merge_page(reader.pages[0])
return pageObj
And here we reach the end of this long tutorial on working with PDF files in python.
Now, you can easily create your own PDF manager!
References:
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.