Rok*_*pta 5 python pdf image typeerror converters
我正在尝试在我安装的 ubuntu 服务器中将 pdf 文件转换为图像文件:
我的代码:
from pdf2image import convert_from_path, convert_from_bytes
images = convert_from_path("/home/user/pdf_file.pdf")
# OR
with open("/home/user/pdf_file.pdf") as pdf:
images = convert_from_bytes(pdf.read())
Run Code Online (Sandbox Code Playgroud)
输出
当我使用函数“convert_from_path”时
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pdf2image/pdf2image.py", line 143, in convert_from_path
thread_output_file = next(output_file)
TypeError: ThreadSafeGenerator object is not an iterator
Run Code Online (Sandbox Code Playgroud)
当我使用函数“convert_from_bytes”时
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib/python2.7/dist-packages/pdf2image/pdf2image.py", line 268, in convert_from_bytes
paths_only=paths_only,
File "/usr/local/lib/python2.7/dist-packages/pdf2image/pdf2image.py", line 143, in convert_from_path
thread_output_file = next(output_file)
TypeError: ThreadSafeGenerator object is not an iterator
Run Code Online (Sandbox Code Playgroud)
我已经重新安装了所有实用程序,然后我遇到了这些问题。
如果你想将 PDF 转换为图像,你可以尝试Python Ghostscript 包:
pip install ghostscript
import ghostscript
import locale
def pdf2jpeg(pdf_input_path, jpeg_output_path):
args = ["pef2jpeg", # actual value doesn't matter
"-dNOPAUSE",
"-sDEVICE=jpeg",
"-r144",
"-sOutputFile=" + jpeg_output_path,
pdf_input_path]
encoding = locale.getpreferredencoding()
args = [a.encode(encoding) for a in args]
ghostscript.Ghostscript(*args)
pdf2jpeg(
"...Fixate/ActiveState/pdf/a.pdf",
"...Fixate/ActiveState/pdf/a.jpeg",
)
Run Code Online (Sandbox Code Playgroud)
我在python2中也失败了,但在python3中成功了。
其他库上也发生了同样的问题: TypeError: 'threadsafe_iter' object is not an iterator
正如他们所说,这是一个 python 2 vs 3 问题,由 next() 函数引起。
如果在文件中修改__next__()-> ,则在py2中运行成功。next()/home/***/.local/lib/python2.7/site-packages/pdf2image/generators.py
顺便说一句,我已经向 pdf2image 团队创建了一个新问题。
TypeError:ThreadSafeGenerator 对象不是迭代器 #133
附加的
pdf2image 自述文件说它是一个 python (3.5+) 模块。
pdf2image v1.7.1 在 py27 上工作。尝试一下pip install pdf2image==1.7.1