我已使用此代码将pdf转换为文本。
input1 = '//Home//Sai Krishna Dubagunta.pdf'
output = '//Home//Me.txt'
os.system(("pdftotext %s %s") %( input1, output))
Run Code Online (Sandbox Code Playgroud)
我已经创建了Home目录,并将源文件粘贴到其中。
我得到的输出是
1
Run Code Online (Sandbox Code Playgroud)
并没有创建带有.txt的文件。问题出在哪里?
你的表情
("pdftotext %s %s") %( input1, output)
Run Code Online (Sandbox Code Playgroud)
将翻译为
pdftotext //Home//Sai Krishna Dubagunta.pdf //Home//Me.txt
Run Code Online (Sandbox Code Playgroud)
这意味着传递给的第一个参数pdftotext是//Home//Sai,第二个参数是Krishna。这显然行不通。
将参数括在引号中:
os.system("pdftotext '%s' '%s'" % (input1, output))
Run Code Online (Sandbox Code Playgroud)
有多种Python软件包可使用Python从PDF提取文本。
pdftotext 包:似乎工作得很好,但它没有其他选择,例如提取边界框
对于Ubuntu:
sudo apt-get install build-essential libpoppler-cpp-dev pkg-config python-dev
Run Code Online (Sandbox Code Playgroud)
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())
Run Code Online (Sandbox Code Playgroud)
使用进行安装pip install pdfminer.six。这里是一个最小的工作示例。