使用 Python 中的已归档字段从 PDF 复制部分

问题描述

我会尽量描述这个过程。

  1. 在交互式 PDF 中使用以下代码填写字段“Textovépole60”,值为“123456789”并保存:
from PyPDF4 import PdfFileWriter,PdfFileReader
from PyPDF4.generic import BooleanObject,NameObject,IndirectObject

def set_need_appearances_writer(writer: PdfFileWriter):
    try:
        catalog = writer._root_object
        # get the AcroForm tree
        if "/AcroForm" not in catalog:
            writer._root_object.update({
                NameObject("/AcroForm"): IndirectObject(len(writer._objects),writer)})

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
        return writer

    except Exception as e:
        print('set_need_appearances_writer() catch : ',repr(e))
        return writer

infile = "DOTAZNIK_ADULT.pdf"
outfile = "DOTAZNIK_ADULT_VYPLNENY.pdf"

inputStream = open(infile,"rb")
pdf = PdfFileReader(inputStream,strict=False)
if "/AcroForm" in pdf.trailer["/Root"]:
    pdf.trailer["/Root"]["/AcroForm"].update(
        {NameObject("/NeedAppearances"): BooleanObject(True)})

pdf2 = PdfFileWriter()
set_need_appearances_writer(pdf2)
if "/AcroForm" in pdf2._root_object:
    pdf2._root_object["/AcroForm"].update(
        {NameObject("/NeedAppearances"): BooleanObject(True)})

field_dictionary = {"Textové pole60": "123456789"}

pdf2.addPage(pdf.getPage(0))
pdf2.updatePageFormFieldValues(pdf2.getPage(0),field_dictionary)

outputStream = open(outfile,"wb")
pdf2.write(outputStream)
inputStream.close()
outputStream.close()
  1. 然后,当我在 adobe reader 中打开 PDF 时,值会在那里填写: Filled field

  2. 然后我想将页面从 PDF 转换为图像,但是我没有使用以下代码在此处填写值 After run script and show pil_im in spyder

import pdf2image import PyTesseract from PyTesseract import Output

PyTesseract.PyTesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

pdf_path = "DOTAZNIK_ADULT_VYPLNENY.pdf"

images = pdf2image.convert_from_path(pdf_path,poppler_path = 'C:\\Program Files\\Poppler\\bin')

pil_im = images[0]

请帮帮我! :) 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...