使用它时出现意外错误。第一部分来自我在网上找到的脚本,我试图用它来提取 PDF 大纲中标识的特定部分。一切正常,除了output.write(outputfile1)它说:
PdfReadError:字典中有多个定义。
还有人遇到这个吗?最后请原谅所有不必要的prints。:)
import pyPdf
import glob
class Darrell(pyPdf.PdfFileReader):
def getDestinationPageNumbers(self):
def _setup_outline_page_ids(outline, _result=None):
if _result is None:
_result = {}
for obj in outline:
if isinstance(obj, pyPdf.pdf.Destination):
_result[(id(obj), obj.title)] = obj.page.idnum
elif isinstance(obj, list):
_setup_outline_page_ids(obj, _result)
return _result
def _setup_page_id_to_num(pages=None, _result=None, _num_pages=None):
if _result is None:
_result = {}
if pages is None:
_num_pages = []
pages = self.trailer["/Root"].getObject()["/Pages"].getObject()
t = pages["/Type"]
if t == "/Pages":
for page in pages["/Kids"]:
_result[page.idnum] = len(_num_pages) …Run Code Online (Sandbox Code Playgroud) 我实际上是使用pyPdf来打开,读取和写入PDF文件的内容.
为此我使用这些代码行:
from pyPdf import PdfFileWriter, PdfFileReader
pdf = PdfFileReader(file("/myPdfFile.pdf", "w+b"))
content = pdf.getPage(1).extractText()
print content
Run Code Online (Sandbox Code Playgroud)
但它返回给我这个错误,我不明白为什么
File "/usr/local/lib/python2.6/dist-packages/pyPdf/pdf.py", line 374, in __init__
self.read(stream)
File "/usr/local/lib/python2.6/dist-packages/pyPdf/pdf.py", line 702, in read
stream.seek(-1, 2)
IOError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
我正在尝试将PDF的每个页面提取为字符串:
import pyPdf
pages = []
pdf = pyPdf.PdfFileReader(file('g-reg-101.pdf', 'rb'))
for i in range(0, pdf.getNumPages()):
this_page = pdf.getPage(i).extractText() + "\n"
this_page = " ".join(this_page.replace(u"\xa0", " ").strip().split())
pages.append(this_page.encode("ascii", "xmlcharrefreplace"))
for page in pages:
print '*' * 80
print page
Run Code Online (Sandbox Code Playgroud)
但是这个脚本忽略了换行符,让我看起来像乱码information concerning an individual which, because of name, identifyingnumber, mark or description(比如,这应该读identifying number,不是identifyingumber).
我想使用pypdf遍历pdf页面中的所有对象。
我还想检查对象的类型是什么,无论是文本还是图形。
代码段将提供很大的帮助。
非常感谢
我想在 pdf 的每一页上都有一个文本。这段文本是一个 html 代码,看起来<p style="color: #ff0000">blabla</p>在最终文档中显示为红色,我将其转换为 pdf(html2pdf lib),然后将它(PyPDF2 lib)合并到我的 pdf 的每一页。...但合并非常缓慢!
我的问题是:有没有比 PyPDF2 的 page.mergePage 方法更快的合并 pdf 的方法?(或者也许有更快的方法将我的文本添加到此 pdf 中?)
谢谢 !(在 Windows 8 上使用 python 2.7.5)
嗨,我刚刚开始使用python并尝试安装一些必需的库。在OS X上使用Python 3.4.1。我已经安装了PyPDF2(假设成功),但似乎无法使用这些工具:
sh-3.2# port select --list python
Available versions for python:
none
python25-apple
python26
python26-apple
python27-apple
python34 (active)
sh-3.2# pip install PyPDF2
Requirement already satisfied (use --upgrade to upgrade): PyPDF2 in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
Cleaning up...
sh-3.2#
Run Code Online (Sandbox Code Playgroud)
...
import PyPDF2
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import PyPDF2
ImportError: No module named 'PyPDF2'
>>>
Run Code Online (Sandbox Code Playgroud)
我错过了一步吗?还是py3.4.1不支持PyPDF2?
我pyPdf用来从PDF中提取文本.我希望能够知道哪个文本是粗体,以便识别粗体节标题.如何识别粗体文字?
import PyPDF2
import glob
import os
from fpdf import FPDF
import shutil
class MyPDF(FPDF): # adding a footer, containing the page number
def footer (self):
self.set_y(-15)
self.set_font("Arial", Style="I", size=8)
pageNum = "page %s/{nb}" % self.page_no()
self.cell(0,10, pageNum, align="C")
if __name__ == "__main__":
os.chdir("pathtolocation/docs/") # docs location
os.system("libreoffice --headless --invisible --convert-to pdf *") # this converts everything to pdf
for file in glob.glob("*"):
if file not in glob.glob("*.pdf"):
shutil.move(file,"/newlocation") # moving files we don't need to another folder
# adding the …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 PyFPDF 生成 PDF。我还需要添加一个垂直/旋转的文本。不幸的是,据我所知,PyPDF 并不直接支持它。有适用于 PHP 的 FPDF 解决方案。
有没有办法使用 PyFPDF 或其他库从 Python 在 PDF 中插入垂直或旋转文本?
代码是
from PyPDF2 import PdfFileReader
with open('HTTP_Book.pdf','rb') as file:
pdf=PdfFileReader(file)
pagedd=pdf.getPage(0)
print(pagedd.extractText())
Run Code Online (Sandbox Code Playgroud)
此代码引发如下所示的错误:
TypeError: ord() expected string of length 1, but int found
Run Code Online (Sandbox Code Playgroud)
我在互联网上搜索并发现了此故障排除“TypeError:ord() 预期长度为 1 的字符串,但找到了 int”, 但它没有多大帮助。我知道这个错误的背景是什么,但不确定它在这里有什么关系?
尝试更改pdf文件,它工作正常。那么有什么问题:pdf文件或PyPDF2无法处理呢?我知道根据文档,这种方法不太可靠:
这对某些 PDF 文件效果很好,但对其他文件效果不佳,具体取决于所使用的生成器
这应该如何处理?
追溯:
Traceback (most recent call last):
File "pdf_reader.py", line 71, in <module>
print(pagedd.extractText())
File "C:\Users\Jeet\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 2595, in ex
tractText
content = ContentStream(content, self.pdf)
File "C:\Users\Jeet\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 2673, in __
init__
stream = BytesIO(b_(stream.getData()))
File "C:\Users\Jeet\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\generic.py", line 841, in
getData
decoded._data = filters.decodeStreamData(self)
File "C:\Users\Jeet\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\filters.py", …Run Code Online (Sandbox Code Playgroud) pypdf ×10
python ×10
pdf ×5
file ×1
fpdf ×1
import ×1
install ×1
merge ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
string ×1
unicode ×1