我正在努力使用 PyPDF2 模块压缩我合并的 pdf。这是我基于http://www.blog.pythonlibrary.org/2012/07/11/pypdf2-the-new-fork-of-pypdf/ 的尝试
import PyPDF2
path = open('path/to/hello.pdf', 'rb')
path2 = open('path/to/another.pdf', 'rb')
merger = PyPDF2.PdfFileMerger()
merger.append(fileobj=path2)
merger.append(fileobj=path)
pdf.filters.compress(merger)
merger.write(open("test_out2.pdf", 'wb'))
Run Code Online (Sandbox Code Playgroud)
我收到的错误是
TypeError: must be string or read-only buffer, not file
Run Code Online (Sandbox Code Playgroud)
我还尝试在合并完成后压缩 pdf。我将失败的压缩基于使用 PDFSAM 压缩后得到的文件大小。有什么想法吗?谢谢。
我正在尝试将 pdf 拆分为其页面并将每个页面另存为新的 pdf。我从上一个问题尝试过这种方法,但没有成功,这里的 pypdf2 拆分示例也没有成功。编辑:我可以在我的文件中看到它成功写入了第一页,然后创建了第二页 pdf 但它是空的。
这是我试图运行的代码:
from PyPDF2 import PdfFileWriter, PdfFileReader
inputpdf = PdfFileReader(open("my_pdf.pdf", "rb"))
for i in range(inputpdf.numPages):
output = PdfFileWriter()
output.addPage(inputpdf.getPage(i))
with open("document-page%s.pdf" % i, "wb") as outputStream:
output.write(outputStream)
Run Code Online (Sandbox Code Playgroud)
这是完整的错误消息:
Traceback (most recent call last):
File "pdf_functions.py", line 9, in <module>
output.write(outputStream)
File "/usr/local/lib/python3.4/dist-packages/PyPDF2/pdf.py", line 482, in write
self._sweepIndirectReferences(externalReferenceMap, self._root)
File "/usr/local/lib/python3.4/dist-packages/PyPDF2/pdf.py", line 572, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "/usr/local/lib/python3.4/dist-packages/PyPDF2/pdf.py", line 548, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/usr/local/lib/python3.4/dist-packages/PyPDF2/pdf.py", line …Run Code Online (Sandbox Code Playgroud) +----+-----------------------------+
| id | name |
+====+=============================+
| 47 | Some textjogjwojgopwgpowmok |
+----+-----------------------------+
| 47 | Some textjogjwojgopwgpowmokg|
+----+-----------------------------+
| 47 | Some textjogjwojgopwgpowmokg|
+----+-----------------------------+
| 47 | Some textjogjwojgopwgpowmokg|
+----+-----------------------------+
| 47 | Some textjogjwojgopwgpowmokg|
+----+-----------------------------+
Run Code Online (Sandbox Code Playgroud)
我想使用python库以上述格式将上表写入PDF文件。感谢立即响应。
我需要批量填写pdf表单,所以我试着写一个python代码从csv文件中为我做.我在这个问题中使用了第二个答案并且它填写了表格,但是当我打开填写的表格时,除非选择相应的字段,否则答案不会显示.表格打印时也没有显示答案.我查看了PyPDF2文档,看看我是否可以压缩生成的表单但是这个功能还没有实现,即使大约一年前已经被要求了.我的偏好是不使用pdftk所以我可以编译脚本而不需要更多的依赖.在上述问题中使用原始代码时,某些字段会显示在打印件中,有些字段不会让我对它们的工作方式感到困惑.任何帮助表示赞赏.
这是代码.
# -*- coding: utf-8 -*-
from collections import OrderedDict
from PyPDF2 import PdfFileWriter, PdfFileReader
def _getFields(obj, tree=None, retval=None, fileobj=None):
"""
Extracts field data if this PDF contains interactive form fields.
The *tree* and *retval* parameters are for recursive use.
:param fileobj: A file object (usually a text file) to write
a report to on all interactive form fields found.
:return: A dictionary where each key is a field name, and each
value is a :class:`Field<PyPDF2.generic.Field>` object. …Run Code Online (Sandbox Code Playgroud) 我有一个项目,我需要填写预制的 PDF,我想到的最合乎逻辑的解决方案是将预制的 PDF 制作成 PDF 表单,以便输入值应该放在标签中,然后我可以查看 PDF 中的表单标签,并将它们与值字典对齐。
我已经使用PyPDF2完成了这项工作。总的来说,我拍了一张网络表单的图像,然后打开 Acrobat 并根据图像中看到的字段创建了一个 PDF 表单,然后使用PyPDF2来填写 PDF 表单字段,但需要注意的是打印那些填写的值似乎在某些浏览器中存在问题,Firefox 就是其中之一。
我如何将我的 PDF 表单转换为标准/平面 PDF 以便我可以保留预先填充的值,但会丢失可编辑的字段(因为我认为这是问题所在)?
from io import BytesIO
import PyPDF2
from django.http import HttpResponse
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject
def pdf_view(request):
template = 'templates/template.pdf'
outfile = "templates/test.pdf"
input_stream = open(template, "rb")
pdf_reader = PyPDF2.PdfFileReader(input_stream, strict=False)
if "/AcroForm" in pdf_reader.trailer["/Root"]:
pdf_reader.trailer["/Root"]["/AcroForm"].update(
{NameObject("/NeedAppearances"): BooleanObject(True)})
pdf_writer = PyPDF2.PdfFileWriter()
set_need_appearances_writer(pdf_writer)
if "/AcroForm" in pdf_writer._root_object:
# Acro form is form field, …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 pdf 文件中获取文本。下面是代码:
from PyPDF2 import PdfFileReader
with open('HTTP_Book.pdf', 'rb') as file:
pdf = PdfFileReader(file)
page = pdf.getPage(1)
#print(dir(page))
print(page.extractText())
Run Code Online (Sandbox Code Playgroud)
这给了我错误
ValueError: seek of closed file
Run Code Online (Sandbox Code Playgroud)
我只是将代码放在with语句下,它工作正常。我的问题是:为什么会这样?我已经将信息存储在“pdf”对象中,所以我应该能够在块外访问它。
我想将元数据键值对添加到pdf文件的元数据中.
我找到了几年的答案,但我认为这是复杂的方法.我想今天有一种更简单的方法:https://stackoverflow.com/a/3257340/633961
我没有和pypdf2结婚,如果有更简单的方法,那我就这样走吧?
我有一个 pdf 文件列表,我需要在这些文件的每一页上突出显示特定文本,并为每个文本实例保存一个快照。
到目前为止,我能够突出显示文本并将pdf文件的整个页面保存为快照。但是,我想找到突出显示文本的位置并放大快照,与整页快照相比,它会更详细。
我很确定这个问题一定有解决方案。我是 Python 的新手,因此我找不到它。如果有人能帮我解决这个问题,我将不胜感激。
我曾尝试使用PyPDF2,Pymupdf库,但我无法找出解决方案。我还尝试通过提供有效的坐标来突出显示,但找不到将这些坐标作为输出获取的方法。
[![Sample snapshot from the code[![\]\[1\]][1]][1]][1]
#import PyPDF2
import os
import fitz
from wand.image import Image
import csv
#import re
#from pdf2image import convert_from_path
check = r'C:\Users\Pradyumna.M\Desktop\Pradyumna\Automation\Intel Bytes\Create Source Docs\Sample Check 8 Apr 2019'
dir1 = check + '\\Source Docs\\'
dir2 = check + '\\Output\\'
dir = [dir1, dir2]
for x in dir:
try:
os.mkdir(x)
except FileExistsError:
print("Directory ", x, " already exists")
### READ PDF FILE
with open('upload1.csv', …Run Code Online (Sandbox Code Playgroud) 你们能解决问题吗?我无法阅读阿拉伯语 PDF 文件。我不知道是什么问题。谢谢
import PyPDF2
def main():
with open("arabic_text.pdf", encoding='utf-8') as pdfFile:
pdfRead = PyPDF2.PdfFileReader(pdfFile)
output = PdfFileWriter()
for m in range(pdfRead.getNumPages()):
page = pdfRead.getPage(m)
pageContent = page.extractText()
print(pageContent)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud) 我是 Python 和一般编码的新手。我正在尝试创建一个程序,该程序将对 PDF 目录进行 OCR,然后提取文本,以便稍后我可以挑选出特定的内容。但是,我无法让 pdfPlumber 从所有页面中提取所有文本。您可以从开始到结束建立索引,但如果结束未知,则会因为索引超出范围而中断。
import ocrmypdf
import os
import requests
import pdfplumber
import re
import logging
import sys
import PyPDF2
## test folder C:\Users\adams\OneDrive\Desktop\PDF
user_direc = input("Enter the path of your files: ")
#walks the path and prints out each PDF in the
#OCRs the documents and skips any OCR'd pages.
for dir_name, subdirs, file_list in os.walk(user_direc):
logging.info(dir_name + '\n')
os.chdir(dir_name)
for filename in file_list:
file_ext = os.path.splitext(filename)[0--1]
if file_ext == '.pdf':
full_path = dir_name + …Run Code Online (Sandbox Code Playgroud) pypdf2 ×10
python ×8
pdf ×6
python-3.x ×3
pdfrw ×2
django ×1
pdf-form ×1
python-2.7 ×1
reportlab ×1