How to convert paragraph to table?

Viewed 52

input:

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. Routine Supplies - The hospital basic room and critical care area room (emergency department, observation, treatment room, cardiac, medical, surgical, pediatric, respiratory, burn, neonate (level III and IV), neurological, rehabilitative,post-anesthesia or recovery, andtrauma) daily charge shall include all of the following services, personal care and supply items and equipment

2. Medical Equipment- The hospital basic room and critical care area room(emergency department, observation, treatment room, cardiac,medical, surgical, pediatric,respiratory, burn, neonate (level III and IV), neurological, rehabilitative, post- anesthesia or recovery, andtrauma) daily charge shall include all of the following services, personal care and supply items and equipment

This is my code but it not give accurate output that I want

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")
            demo.append(str(re.findall(r'(\d+\.\s.*\n?)+', clean_text.extract_text())).replace('[]', ' '))
        except IndexError:
            print("")
            break
       

Output that I want in CSV or table

Section                            description

Routine Supplies                The hospital basic room and critical care area room (emergency 
                                department, observation, treatment room, cardiac, medical, 
                                surgical, pediatric, respiratory, burn, neonate (level III and 
                                IV), neurological, rehabilitative,post-anesthesia or recovery, 
                                andtrauma) daily charge shall include all of the following 
                                services, personal care and supply items and equipment

Medical Equipment               The hospital basic room and critical care area room(emergency 
                                department, observation, treatment room, cardiac,medical, 
                                surgical, pediatric,respiratory, burn, neonate (level III and 
                                IV), neurological, rehabilitative, post- anesthesia or 
                                recovery, andtrauma) daily charge shall include all of the 
                                following services, personal care and supply items and 
                                equipment 
1 Answers

If you just want a set of rows with two items, then you need to have your regular expression identify the items you want. If I store your text in text, then this works:

text = """\
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. Routine Supplies - The hospital basic room and critical care area room (emergency department, observation, treatment room, cardiac, medical, surgical, pediatric, respiratory, burn, neonate (level III and IV), neurological, rehabilitative,post-anesthesia or recovery, andtrauma) daily charge shall include all of the following services, personal care and supply items and equipment

2. Medical Equipment- The hospital basic room and critical care area room(emergency department, observation, treatment room, cardiac,medical, surgical, pediatric,respiratory, burn, neonate (level III and IV), neurological, rehabilitative, post- anesthesia or recovery, andtrauma) daily charge shall include all of the following services, personal care and supply items and equipment"""

import re
from pprint import pprint
pattern = "\d+\.\s*([^-]*)- (.*)"
demo = re.findall(pattern, text)
pprint(demo)

Output:

[('Routine Supplies ',
  'The hospital basic room and critical care area room (emergency department, '
  'observation, treatment room, cardiac, medical, surgical, pediatric, '
  'respiratory, burn, neonate (level III and IV), neurological, '
  'rehabilitative,post-anesthesia or recovery, andtrauma) daily charge shall '
  'include all of the following services, personal care and supply items and '
  'equipment'),
 ('Medical Equipment',
  'The hospital basic room and critical care area room(emergency department, '
  'observation, treatment room, cardiac,medical, surgical, '
  'pediatric,respiratory, burn, neonate (level III and IV), neurological, '
  'rehabilitative, post- anesthesia or recovery, andtrauma) daily charge shall '
  'include all of the following services, personal care and supply items and '
  'equipment')]

And please note that it's only broken between lines there because of pprint. Each tuple contains two things: the heading, and the paragraph text.

Related