【发布时间】:2014-07-12 08:09:08
【问题描述】:
我已使用此代码将 pdf 转换为文本。
input1 = '//Home//Sai Krishna Dubagunta.pdf'
output = '//Home//Me.txt'
os.system(("pdftotext %s %s") %( input1, output))
我已经创建了主目录并将源文件粘贴到其中。
我得到的输出是
1
并且没有创建带有 .txt 的文件。问题出在哪里?
【问题讨论】:
我已使用此代码将 pdf 转换为文本。
input1 = '//Home//Sai Krishna Dubagunta.pdf'
output = '//Home//Me.txt'
os.system(("pdftotext %s %s") %( input1, output))
我已经创建了主目录并将源文件粘贴到其中。
我得到的输出是
1
并且没有创建带有 .txt 的文件。问题出在哪里?
【问题讨论】:
有多种 Python 包可以使用 Python 从 PDF 中提取文本。
pdftotext 包:似乎工作得很好,但它没有选项,例如提取边界框
对于 Ubuntu:
sudo apt-get install build-essential libpoppler-cpp-dev pkg-config python-dev
import pdftotext
with open("lorem_ipsum.pdf", "rb") as f:
pdf = pdftotext.PDF(f)
# Iterate over all the pages
for page in pdf:
print(page)
# Just read the second page
print(pdf.read(2))
# Or read all the text at once
print(pdf.read_all())
使用pip install pdfminer.six 安装它。一个最小的工作示例是here。
【讨论】:
pdftotext 需要您 first install poppler, which is a little painful on Windows
你的表情
("pdftotext %s %s") %( input1, output)
将转换为
pdftotext //Home//Sai Krishna Dubagunta.pdf //Home//Me.txt
表示传递给pdftotext的第一个参数是//Home//Sai,第二个参数是Krishna。这显然行不通。
用引号将参数括起来:
os.system("pdftotext '%s' '%s'" % (input1, output))
【讨论】:
pdftotext '//Home//Sai Krishna Dubagunta.pdf' '//Home//Me.txt' 会发生什么?
"/"和反斜杠"\"吗?
我认为 pdftotext 命令只需要一个参数。尝试使用:
os.system(("pdftotext %s") % input1)
看看会发生什么。希望这会有所帮助。
【讨论】: