Я прочитал, что встроенный файл должен находиться в /EmbeddedFiles
если я попытаюсь получить эти даты через этот сайт
https:/ /eforms.aloaha.com/zugferd.aspx
Потом я получил XML-файл, но если я попытаюсь получить эти данные через свою функцию, программа напишет -> Указанный PDF-файл не содержит встроенного XML-файл ZUGFeRD.
это то, что я пробовал
Код: Выделить всё
def extract_zugferd_xml(pdf_file_path):
"""
Extracts the ZUGFeRD XML from a PDF file and returns it as a string.
Args:
pdf_file_path (str): The path to the PDF file to extract the XML from.
Returns:
str: The ZUGFeRD XML as a string.
"""
# Open the PDF file in binary mode using PyPDF2
with open(pdf_file_path, 'rb') as pdf_file:
# Create a PDF reader object
pdf_reader = PyPDF2.PdfReader(pdf_file)
# Get the number of pages in the PDF file
# num_pages = pdf_reader.getNumPages()
num_pages = len(pdf_reader.pages)
# Loop through each page in the PDF file
for page_num in range(num_pages):
# Get the page object for the current page
# page = pdf_reader.getPage(page_num)
page = pdf_reader.pages[page_num]
print("PAGE ", page)
print("MediaBox ", page['/MediaBox'])
print("Parent ", page['/Parent']['/Kids'][0])
print("Parent type", type(page['/Parent']['/Kids']))
# Create a PDF reader object
pdf_reader = PyPDF2.PdfReader(pdf_file)
# Get the indirect reference object
indirect_ref = page['/Parent']['/Kids'][0]
# Get the referenced object directly
ref_obj = pdf_reader.get_object(indirect_ref)
# Print the content of the object
print("ref_obj ", ref_obj)
# Check if the page has any embedded files
if '/EmbeddedFiles' in page:
# Get the embedded files dictionary
embedded_files = page['/EmbeddedFiles']
# Loop through each entry in the embedded files dictionary
for file_name, file_obj in embedded_files.items():
# Check if the file is a ZUGFeRD XML file
if file_name.endswith('.xml') and 'ZUGFeRD' in file_obj.getData():
# Extract and return the XML data as a string
return file_obj.getData().decode('utf-8')
# If the function reaches this point, it means the PDF file does not contain an embedded ZUGFeRD XML file
raise ValueError('The specified PDF file does not contain an embedded ZUGFeRD XML file.')
Код: Выделить всё
PAGE {'/Type': '/Page', '/MediaBox': [0, 0, 595, 842], '/Rotate': 0, '/Parent': IndirectObject(3, 0, 4370055712), '/Resources': {'/ProcSet': ['/PDF', '/Text'], '/Font': IndirectObject(21, 0, 4370055712)}, '/Contents': IndirectObject(11, 0, 4370055712)}
MediaBox [0, 0, 595, 842]
Parent IndirectObject(10, 0, 4370055712)
Parent type
Подробнее здесь: https://stackoverflow.com/questions/792 ... df-zugferd