本文提供了使用Python从PDF表单中提取数据的指南。它包含设置IDE的所有详细信息、步骤列表以及访问表单字段数据的示例代码。示例代码将创建一个包含字段和值的测试PDF,并从所有字段中提取数据。
使用Python从PDF表单字段中提取数据的步骤
- 设置使用Aspose.PDF for Python via .NET提取表单数据的环境
- 创建或加载包含数据的输入字段的PDF文件到Document对象中
- 从加载的PDF文档的form属性中获取所有字段
- 遍历所有字段并访问每个字段
- 显示字段的完整名称和值
这些步骤描述了如何使用Python从可填写PDF中提取数据。创建或加载包含字段和值的PDF文件,并从PDF文件中的Form属性访问字段集合。遍历所有字段并访问完整名称和值以进行进一步处理。
使用Python从PDF中提取表单字段的代码
import aspose.pdf as pdf | |
from aspose.pdf import Document, License, Rectangle | |
from aspose.pdf.forms import TextBoxField | |
def main(): | |
# Load Aspose PDF license | |
license = License() | |
license.set_license("license.lic") | |
# Generate PDF with input fields | |
create_pdf_with_fields() | |
# Open and process the generated PDF file | |
pdf_document = Document("UserForm.pdf") | |
# Retrieve and display form fields | |
form_fields = pdf_document.form.fields | |
for form_field in form_fields: | |
print("Field Name:", form_field.full_name) | |
print("Field Content:", form_field.value) | |
def create_pdf_with_fields(): | |
# Instantiate new PDF document | |
pdf_file = Document() | |
for page_index in range(1, 4): # 3 pages | |
new_page = pdf_file.pages.add() | |
for field_index in range(1, 5): # 4 fields per page | |
# Define a text input field | |
input_field = TextBoxField(new_page, Rectangle(120, field_index * 90, 320,(field_index + 1) * 90,True)) | |
input_field.partial_name = f"inputField_{page_index}_{field_index}" | |
input_field.value = f"Data Entry {page_index}-{field_index}" | |
# Attach field to the document form | |
pdf_file.form.add(input_field, page_index) | |
# Save document to disk | |
pdf_file.save("UserForm.pdf") | |
main() |
此代码演示了如何从PDF表单中提取数据。我们使用了Document.form.fields集合,该集合包含PDF中的所有字段。您可以使用从集合中访问的Field对象中的page_index来过滤特定页面中的字段。
本文教会了我们读取PDF表单数据的过程。如果您想将PDF文件扁平化,请参阅如何在Python中扁平化PDF一文。