I am using the below code to try and extract text out of a PDF form that was first made in MS Word and then converted to a form in Acrobat Pro. In doc properties it says Acrobat PDFMaker 22.
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import resolve1
from pdfminer.psparser import PSLiteral, PSKeyword
from pdfminer.utils import decode_text
file_path = r'myPDF.pdf'
data = {}
def decode_value(value):
# decode PSLiteral, PSKeyword
if isinstance(value, (PSLiteral, PSKeyword)):
value = value.name
# decode bytes
if isinstance(value, bytes):
value = decode_text(value)
return value
with open(file_path, 'rb') as fp:
parser = PDFParser(fp)
doc = PDFDocument(parser)
res = resolve1(doc.catalog)
if 'AcroForm' not in res:
raise ValueError("No AcroForm Found")
fields = resolve1(doc.catalog['AcroForm'])['Fields'] # may need further resolving
p = Path(file_path)
print(f'Form name# {p.stem}')
for f in fields:
# extract normal text
# text = extract_text(file_path)
# print(text)
field = resolve1(f)
name, values = field.get('T'), field.get('V')
# decode name
name = decode_text(name)
# resolve indirect obj
values = resolve1(values)
# decode value(s)
if isinstance(values, list):
values = [decode_value(v) for v in values]
else:
values = decode_value(values)
data.update({name: values})
print(name, values)
I am not able to extract the text and get this error.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
C:\Users\GOLDSB~1\AppData\Local\Temp/ipykernel_20804/1611217105.py in <module>
24
25 if 'AcroForm' not in res:
---> 26 raise ValueError("No AcroForm Found")
27
28 fields = resolve1(doc.catalog['AcroForm'])['Fields'] # may need further resolving
ValueError: No AcroForm Found
Here is some details about PDFMaker https://helpx.adobe.com/acrobat/using/creating-pdfs-pdfmaker-windows.html
I should add that this code works fine when using forms made only in Acrobat Pro, in doc properties it says Adobe Acrobat Pro DC
Is there anyway to get this to work? I would like to stick with starting the forms in word and turning them into forms in Acrobat Pro. But I need to be able to extract the text to a database.
-- Edit --
Here is what my test form looks like and it goes through three steps:-
- produce text in MS Word
- convert to PDF in MS Word
- prepare form in Acrobat Pro
