简体   繁体   中英

How to convert an Iterator into Pandas DataFrame?

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!

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)  

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