如何解决使用 Python 中的已归档字段从 PDF 复制部分
我会尽量描述这个过程。
- 在交互式 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()
-
然后,当我在 adobe reader 中打开 PDF 时,值会在那里填写: Filled field
-
然后我想将页面从 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 举报,一经查实,本站将立刻删除。