Search code examples
pythonnlppython-re

How to extract Bold text from pdf using python?


The list below provides examples of items and services that should not be billed separately. Please note that the list is not all inclusive.

1. Surgical rooms and services – To include surgical suites, major and minor, treatment rooms, endoscopy labs, cardiac cath labs, X-ray.

2. Facility Basic Charges - pulmonary and cardiology procedural rooms. The hospital’s charge for surgical suites and services shall include the entire above listed nursing personnel services, supplies, and equipment

I want output like:

  1. Surgical rooms and services
  2. Facility Basic Charges

there is first sentence also bold but we need to omit that sentence, we need to extract only those text which are represented with numbers


Solution

  • Use This Code:

    import pdfplumber
    import re
    demo = []
    with pdfplumber.open('HCSC IL Inpatient_Outpatient Unbundling Policy- Facility.pdf') as pdf: 
        for i in range(0, 50):
            try:
                text = pdf.pages[i]  
                clean_text = text.filter(lambda obj: obj["object_type"] == "char" and "Bold" in obj["fontname"])
                demo.append(str(re.findall(r'(\d+\.\s.*\n?)+', clean_text.extract_text())).replace('[]', ' '))
            except IndexError:
                print("")
                break