-1
I am using the code below to read Pdfs with the Tkinter interface:
import io
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage
from tkinter import *
from tkinter import filedialog
janela = Tk()
janela.geometry('800x600')
janela.title('Envio de Email automatizado')
inserepdf = Label(janela, text='Buscar pdf')
inserepdf.pack()
def abrirArquivo():
arquivo = filedialog.askopenfilename()
botaoAbrir = Button(janela, text="Abrir Arquivo", command=abrirArquivo)
botaoAbrir.pack()
def extract_text_from_pdf(pdf_path):
resource_manager = PDFResourceManager()
fake_file_handle = io.StringIO()
converter = TextConverter(resource_manager, fake_file_handle)
page_interpreter = PDFPageInterpreter(resource_manager, converter)
with open(arquivo, 'rb') as fh:
for page in PDFPage.get_pages(fh,
caching=True,
check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
# close open handles
converter.close()
fake_file_handle.close()
if text:
return text
if __name__ == '__main__':
texto = str(extract_text_from_pdf(''))
print(texto.split())
janela.mainloop()
However, I cannot make the program read the imported pdf in the open function File() , informs that the file variable does not exist, but I could not define it as global or nonlocal, and I do not know how to use the variable within the extract_text_from_pdf(pdf_path) function. If you can help me, I’d really appreciate it.
Thank you for your help, but as I reported, I can’t declare the global to be variable. When I try how in your code, the following error occurs: with open(file, 'Rb') as Fh: Filenotfounderror: [Errno 2] No such file or directory: ''
– gereiz
well I’m not an expert on Tkinter but I took a closer look at your code and I think it’s more a matter of code structure and organization and mainly of functions, I’ll leave an example here how I organized the code and made it work.
– Juan Caio
Thanks, sorry for the delay! Really, also organized and worked correctly.
– gereiz
You’re welcome!! I’m glad it worked!
– Juan Caio