New: PDF to E-Invoice for Factur-X/ZUGFeRD, and PDF to PDF/A for archive-ready PDF/A-3b.

How to Extract Bold Text from PDF in Python with PDF Extractor Web API

How Do I Extract Bold Text from PDF in Python?

Ever wondered “how do I extract bold text from PDF in Python”? We prepare this step-by-step tutorial with screenshots to teach you how to extract bold text from PDF in Python with PDF Extractor API.

We have this sample PDF that we can use for this tutorial.

Screenshot of Source File
Screenshot of Source File

Here’s what the output for this tutorial looks like.

Screenshot of Output XML
Screenshot of Output XML

Step 1: Download the Python Code Sample

Download the complete PDF-to-XML Python sample using GitHub’s Download raw file button.

Install its dependency:

python -m pip install requests

Set API_KEY to your PDF.co API key. For files in your script’s folder, configure:

SourceFile = "sample.pdf" DestinationFile = "result.xml"

Place your source PDF in the same folder and run:

python ConvertPdfToXMLFromUploadedFile.py

Step 2: Extract Bold Text from the XML

The PDF-to-XML API returns text with font attributes, including fontStyle. Save this complete filtering script as extract_bold_text.py in the same folder:

from pathlib import Path
import xml.etree.ElementTree as ET

root = ET.parse("result.xml").getroot()
bold_text = []

for element in root.iter("text"):
    style = element.get("fontStyle", "")

    if "bold" in style.lower():
        text = "".join(element.itertext()).strip()
        if text:
            bold_text.append(text)

output = Path("bold-text.txt")
output.write_text("\n".join(bold_text), encoding="utf-8")

print(f"Saved {len(bold_text)} bold text entries to {output}")

After the conversion finishes, run:

python extract_bold_text.py

Open bold-text.txt to review the extracted text. This filter selects entries marked as bold in the XML, including combined styles such as bold italic.

You’ve learned how to extract bold text from PDF in Python with PDF Extractor web API.

Video Guide

Related Tutorials

See Related Tutorials