<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jack9012 </title>
    <description>The latest articles on DEV Community by Jack9012  (@jack_du_64a902eb1614b3933).</description>
    <link>https://dev.to/jack_du_64a902eb1614b3933</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4114752%2F72b3d9cb-c234-4065-824d-7f5c0a1a3fc0.png</url>
      <title>DEV Community: Jack9012 </title>
      <link>https://dev.to/jack_du_64a902eb1614b3933</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jack_du_64a902eb1614b3933"/>
    <language>en</language>
    <item>
      <title>Find and Highlight Text in PDF Files with Python</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Tue, 08 Sep 2026 02:06:12 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/find-and-highlight-text-in-pdf-files-with-python-37m6</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/find-and-highlight-text-in-pdf-files-with-python-37m6</guid>
      <description>&lt;p&gt;In document processing, data analysis, content review, and similar workflows, you may often need to search PDF files for specific text and highlight the matching content so that important information can be identified more quickly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft8tskqbu42gx6b02tfab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft8tskqbu42gx6b02tfab.png" alt="Automate PDF Text Highlighting" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python provides a wide range of PDF processing libraries that make this kind of automation relatively easy to implement. In this article, we will introduce two practical approaches for finding and highlighting text in PDF files: exact text matching and regular-expression-based matching.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Library and Environment Setup
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we will use &lt;strong&gt;Free Spire.PDF for Python&lt;/strong&gt; to search for text and add highlight annotations to PDF documents.&lt;/p&gt;

&lt;p&gt;The library provides convenient APIs for page traversal, exact text searching, regular expression matching, and custom highlight colors, so there is no need to manually work with low-level PDF structures.&lt;/p&gt;

&lt;p&gt;One limitation to keep in mind is that the free version supports PDF documents with up to  &lt;strong&gt;10 pages&lt;/strong&gt; . This is generally sufficient for lightweight testing, small documents, and simple document-processing tasks.&lt;/p&gt;

&lt;p&gt;Before getting started, install the required package with pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install spire.pdf.free
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Scenario 1: Find and Highlight Exact Text Across a PDF
&lt;/h2&gt;

&lt;p&gt;This approach is suitable when you already know the exact keyword or phrase you want to locate. The program can iterate through every page in the PDF and highlight all occurrences of the target text.&lt;/p&gt;

&lt;p&gt;For example, the following code searches for the text &lt;code&gt;"cloud service"&lt;/code&gt; throughout the document and highlights every match.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("inpue.pdf")

# Iterate through all pages in the PDF document
for i in range(pdf.Pages.Count):
    page = pdf.Pages.get_Item(i)

    # Create a text finder for the current page
    pdfTextFinder = PdfTextFinder(page)

    # Set the search parameter to find exact matches
    pdfTextFinde.Options.Parameter = TextFindParameter.IgnoreCase

    # Find all occurrences of the target text on the page
    result = pdfTextFinder.Find("cloud service")

    # Highlight all matched text in cyan
    for find in result:
        find.HighLight(Color.get_Cyan())

# Save the processed PDF document
pdf.SaveToFile("output/result.pdf")

# Release document resources
pdf.Close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How the Code Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Load the PDF document&lt;/strong&gt;
A &lt;code&gt;PdfDocument&lt;/code&gt; object is created, and the &lt;code&gt;LoadFromFile()&lt;/code&gt; method is used to open the source PDF.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterate through each page&lt;/strong&gt;
The program loops through all pages in the document to ensure that matching text is not missed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search for the target text&lt;/strong&gt;
A &lt;code&gt;PdfTextFinder&lt;/code&gt; object is created for each page. The &lt;code&gt;Find()&lt;/code&gt; method performs an exact search and returns all matching text fragments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apply highlighting&lt;/strong&gt;
Each matching result is processed with the &lt;code&gt;HighLight()&lt;/code&gt; method. In this example, cyan is used as the highlight color, but it can be replaced with another supported color.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Save and close the document&lt;/strong&gt;
Finally, the modified PDF is saved to a new file, and &lt;code&gt;Close()&lt;/code&gt; is called to release the document resources.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Scenario 2: Highlight Text Using Regular Expressions
&lt;/h2&gt;

&lt;p&gt;In many cases, the text you want to find does not have a fixed value but follows a consistent pattern.&lt;/p&gt;

&lt;p&gt;Typical examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Percentages&lt;/li&gt;
&lt;li&gt;Phone numbers&lt;/li&gt;
&lt;li&gt;Dates&lt;/li&gt;
&lt;li&gt;Email addresses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these situations, regular expressions provide a more flexible way to search for matching text.&lt;/p&gt;

&lt;p&gt;In the following example, we use a regular expression to find and highlight integers, decimal numbers, and percentages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("input.pdf")

# Get the first page of the PDF
# Change the page index if you want to process another page
page = pdf.Pages.get_Item(0)

# Create a text finder for the page
pdfTextFinder = PdfTextFinder(page)

# Enable regular expression matching
pdfTextFinder.Options.Parameter = TextFindParameter.Regex

# Regular expression for integers, decimals, and percentages
# Examples: 10, 99.9, 50%
pattern = r'\d+(?:\.\d+)?%?'

# Find all text fragments that match the pattern
result = pdfTextFinder.Find(pattern)

# Highlight the matched text in deep pink
for find in result:
    find.HighLight(Color.get_DeepPink())

# Save the processed PDF document
pdf.SaveToFile("output/result.pdf")

# Release document resources
pdf.Close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Points
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Enable regular expression mode&lt;/strong&gt;
Set &lt;code&gt;Options.Parameter&lt;/code&gt; to &lt;code&gt;TextFindParameter.Regex&lt;/code&gt; to switch from standard text searching to regular-expression matching.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define the matching pattern&lt;/strong&gt;
The regular expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   \d+(?:\.\d+)?%?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;matches several common numeric formats, including integers, decimal numbers, and percentages.&lt;/p&gt;

&lt;p&gt;You can replace it with another pattern to search for phone numbers, dates, email addresses, or other structured text.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Process a specific page&lt;/strong&gt;
This example searches only the first page of the PDF. If you need to search the entire document, you can combine this approach with the page loop used in the first example.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Customize the highlight color&lt;/strong&gt;
The highlight color can be changed by using another supported color value. This makes it possible to apply different visual styles for different types of matched content.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  4. Common Issues and Optimization Tips
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. The PDF Cannot Be Saved
&lt;/h3&gt;

&lt;p&gt;Make sure that the output directory already exists before saving the file.&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;output&lt;/code&gt; folder does not exist, Python may raise a file-path-related error.&lt;/p&gt;

&lt;p&gt;You can create the directory automatically with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

os.makedirs("output", exist_ok=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The PDF Exceeds the Page Limit
&lt;/h3&gt;

&lt;p&gt;The free version supports PDF documents with up to 10 pages.&lt;/p&gt;

&lt;p&gt;For longer documents, you can split the PDF into smaller files before performing the search and highlight operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Improving Search Accuracy
&lt;/h3&gt;

&lt;p&gt;By default, the search operation may match text fragments containing the specified keyword.&lt;/p&gt;

&lt;p&gt;If you need more precise matching, such as whole-word matching or case-sensitive searching, you can configure the corresponding options through the text finder's &lt;code&gt;Options&lt;/code&gt; settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Conclusion
&lt;/h2&gt;

&lt;p&gt;This article demonstrated two ways to find and highlight text in PDF documents with Python: exact text matching and regular-expression-based matching.&lt;/p&gt;

&lt;p&gt;Exact matching works well when the target keyword is already known, while regular expressions are more suitable for structured or variable content such as numbers, percentages, dates, and other formatted data.&lt;/p&gt;

&lt;p&gt;Both approaches require relatively little code and can be easily integrated into automated document-processing scripts. For small and short PDF files, they provide a straightforward way to identify important content, prepare documents for further data extraction, and reduce the amount of manual document review required.&lt;/p&gt;

</description>
      <category>automation</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
