简体   繁体   中英

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

You can do it using this code:

import pdfplumber
with pdfplumber.open('test.pdf') as pdf: 
    text = pdf.pages[0]
    clean_text = text.filter(lambda obj: obj["object_type"] == "char" and "Bold" in obj["fontname"])
    print(clean_text.extract_text())

It use pdfplumber library, so for more info you can check they documentation

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM