Converting a PDF file to a Text file in Python

故事扮演 提交于 2020-01-14 19:37:47

问题


I've been on it for several days + researching the internet on how to get specific information from a pdf file.

Eventually I was able to fetch all information using Python from a text file(which I created by going to the PDF file -----> File ------> Save as Text).

The question is how do I get Python to accomplish those tasks(Going to the PDF file(opening it - is quite easy open("file path"), clicking on File in the menu, and then saving the file as a text file in the same directory).

Just to be clear, I do not require the pdfminer or pypdf libraries as I have already extracted the information with the same file(after converting it manually to txt)


回答1:


You could use pdftotext.exe that you can download from http://www.foolabs.com/xpdf/download.html and then execute it on your pdf files via Python:

import os
import glob
import subprocess

#remember to put your pdftotxt.exe to the folder with your pdf files 
for filename in glob.glob(os.getcwd() + '\\*.pdf'):
    subprocess.call([os.getcwd() + '\\pdftotext', filename, filename[0:-4]+".txt"])

At least it worked for one of my projects.



来源:https://stackoverflow.com/questions/38496026/converting-a-pdf-file-to-a-text-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!