이 문서는 Python을 사용하여 PDF 양식에서 데이터를 추출하는 방법을 안내합니다. 개발 환경 설정, 단계별 절차, 그리고 양식 필드 데이터를 가져오는 샘플 코드가 포함되어 있습니다. 샘플 코드는 필드와 값을 포함한 테스트 PDF를 생성하고, 모든 필드의 데이터를 추출합니다.
Python을 사용하여 PDF 양식 필드에서 데이터 추출하는 단계
- Aspose.PDF for Python via .NET 환경을 설정하여 양식 데이터를 추출합니다.
- 입력 필드에 데이터가 포함된 PDF 파일을 Document 객체로 생성하거나 로드합니다.
- 로드된 PDF 문서의 form 속성에서 모든 필드를 가져옵니다.
- 모든 필드를 순회하면서 각 필드에 접근합니다.
- 필드의 전체 이름과 값을 표시합니다.
이 단계에서는 Python을 사용하여 작성 가능한 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 평탄화하는 방법 문서를 참조하세요.