微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?