AFAIK, you will have to at least create a temp file so that you can
perform your process.
You can use the following code which takes / reads a PDF file and converts it to a TEXT file.
This makes use of PDFMINER and Python 3.7.
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter,TextConverter,XMLConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
import io
def convert(case,fname, pages=None):
if not pages:
pagenums = set()
else:
pagenums = set(pages)
manager = PDFResourceManager()
codec = 'utf-8'
caching = True
output = io.StringIO()
converter = TextConverter(manager, output, codec=codec, laparams=LAParams())
interpreter = PDFPageInterpreter(manager, converter)
infile = open(fname, 'rb')
for page in PDFPage.get_pages(infile, pagenums, caching=caching, check_extractable=True):
interpreter.process_page(page)
convertedPDF = output.getvalue()
print(convertedPDF)
infile.close()
converter.close()
output.close()
return convertedPDF
Main function to call the above program:
import os
import converter
import sys, getopt
class ConvertMultiple:
def convert_multiple(pdf_dir, txt_dir):
if pdf_dir == "": pdf_dir = os.getcwd() + "\\" # if no pdfDir passed in
for pdf in os.listdir(pdf_dir): # iterate through pdfs in pdf directory
print("File name is %s", os.path.basename(pdf))
file_extension = pdf.split(".")[-1]
print("file extension is %s", file_extension)
if file_extension == "pdf":
pdf_file_name = pdf_dir + pdf
path = 'E:/pdf/' + os.path.basename(pdf)
print(path)
text = converter.convert('text', path) # get string of text content of pdf
text_file_name = txt_dir + pdf + ".txt"
text_file = open(text_file_name, "w") # make text file
text_file.write(text) # write text to text file
pdf_dir = "E:/pdf"
txt_dir = "E:/text"
ConvertMultiple.convert_multiple(pdf_dir, txt_dir)
Of course you can tune it some more and may be some more room for improvement, but this thing certainly works.
Just make sure instead of providing pdf folder provide a temp pdf
file directly.
Hope this helps you..Happy Coding!