Python & PDFs
7 Powerful Ways to Interact with Your Documents Using Fitz
Master the Art of PDF Manipulation with the Fitz Library
PDFs, though commonplace, can be transformed into dynamic assets when combined with Python. With the capabilities of the fitz library, a wide range of functionalities become accessible. Let's explore seven common operations:
1. Splitting a PDF Using Bookmarks:
Library: Fitz
Splitting a PDF based on its bookmarks becomes straightforward with fitz.
import fitz
def split_pdf_by_bookmarks(pdf_path):
doc = fitz.open(pdf_path)
bookmarks = doc.getToC()
for bookmark in bookmarks:
title, _, start_page = bookmark
page = doc.load_page(start_page)
new_pdf = fitz.open()
new_pdf.insert_page(0, image=page.get_pixmap())
new_pdf.save(f"{title}.pdf")2. Extracting Text from PDFs:
Library: Fitz
Fitz allows for efficient text extraction from PDFs.
import fitz
def extract_pdf_text(pdf_path):
doc = fitz.open(pdf_path)
text = ""
for page in doc:
text += page.get_text()
return text3. Extracting Text from Scanned PDFs:
Library: Pytesseract
For scanned PDFs, Pytesseract remains the choice for OCR.
from PIL import Image
import fitz
import pytesseract
def ocr_extract_text(pdf_path):
doc = fitz.open(pdf_path)
page = doc[0]
image = page.get_pixmap().to_image()
text = pytesseract.image_to_string(Image.frombytes("RGB", [image.width, image.height], image.samples))
return text4. Rendering PDFs to Images:
Library: Fitz
Transforming a PDF to an image is also possible with Fitz.
import fitzdef pdf_to_image(pdf_path, image_path):
doc = fitz.open(pdf_path)
page = doc[0]
pixmap = page.get_pixmap()
pixmap.save(image_path)5. Searching for Text in PDFs:
Library: Fitz
Efficiently search for specific text within your PDF.
import fitzdef search_text(pdf_path, query):
doc = fitz.open(pdf_path)
occurrences = []
for page in doc:
found = page.search_for(query)
for occurrence in found:
rect = occurrence.rect
occurrences.append((page.number, rect))
return occurrences6. Annotating PDFs:
Library: Fitz
Adding annotations or comments to a PDF is seamless with Fitz.
def annotate_pdf(pdf_path, output_path, page_num, rect, text):
doc = fitz.open(pdf_path)
page = doc[page_num]
page.add_text_annot(rect, text)
doc.save(output_path)7. Merging Multiple PDFs:
Library: Fitz
Join multiple PDFs into a single document with ease.
def merge_pdfs(pdf_list, output_path):
merged = fitz.open()
for pdf in pdf_list:
doc = fitz.open(pdf)
merged.insert_pdf(doc)
merged.save(output_path)Concluding Thoughts:
The fitz library, or PyMuPDF, indeed opens the door to a wealth of functionalities when it comes to PDF manipulation. Whether extracting text, rendering pages as images, annotating, or even merging multiple documents, fitz stands out as a robust choice for developers.
In Plain English
Thank you for being a part of our community! Before you go:
- Be sure to clap and follow the writer! 👏
- You can find even more content at PlainEnglish.io 🚀
- Sign up for our free weekly newsletter. 🗞️
- Follow us on Twitter(X), LinkedIn, YouTube, and Discord.






