Home > Blockchain >  How to convert an Iterator into Pandas DataFrame?
How to convert an Iterator into Pandas DataFrame?

Time:12-24

I was trying to extract checkbox values from a PDF which I am able to with the help of the code below which I found from a thread in stackoverflow and it was provided by @Fabian.

Python: PDF: How to read from a form with radio buttons

filename = 'Accordd1.pdf'
fp = open(filename, 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
fields = resolve1(doc.catalog['AcroForm'])['Fields']
for i in fields:
    field = resolve1(i)
    name = str(field.get('T'),'utf-8')
    value = field.get('V')
    if value != None:
            value = str(value)
            if value[0] == r"/":
                value = value[2:-1]
                value = str(value)
   print (f'{name}: {value}')

Below is the output I am getting:

Check Box47: None
Check Box48: None
Check Box49: None
Check Box50: None
Check Box51: None
Check Box52: None
Check Box53: None
Check Box54: None
Check Box55: None
Text56: None

I am very new to Python programming and not able to convert this output to a DataFrame as I want to export it into Excel - I tried appending the data into a blank list but it's not giving me correct results. Any help would be much appreciated.

Thank you so much in advance!

CodePudding user response:

IIUC:

import pandas as pd
data = []
for i in fields:
   #Rest of logic
   print (f'{name}: {value}')
   data.append([name, value])

df = pd.DataFrame(data, columns=['name', 'value'])
df.to_excel("output.xlsx", index=False)  
  • Related