Reputation: 35
I use this method :
from PyPDF2 import PdfFileReader
from tkinter import *
from tkinter import filedialog
import os
root = Tk()
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
browsebutton = Button(root, text="Browse", command=browsefunc)
browsebutton.pack()
pathlabel = Label(root)
pathlabel.pack()
def get_info(path):
with open(path, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
print(info)
author = info.author
creator = info.creator
producer = info.producer
subject = info.subject
title = info.title
if __name__ == '__main__':
path = pathlabel.pack()
get_info(path)
mainloop()
However, i get the following error:
TypeError: invalid file: None
How can i get the file from pathlabel
?
Is it possible to open the file from file path?
Upvotes: 2
Views: 700
Reputation: 15226
I see a couple problems here.
Delete this:
if __name__ == '__main__':
path = pathlabel.pack()
get_info(path)
what this is doing is making path
= to the result of pathlabel.pack()
. That result will always be None
as pack()
is always going to return None
. Then you are passing None
to the get_info()
function.
Instead lets call get_info
from your brousefunc()
functions.
Change brousefunc()
to this:
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
get_info(filename)
This will update your label and then also send the correct file path to the get_info
function.
If you still wish to use the label's text for the file path you can do path = pathlabel["text"]
as this will assign the value of the text to the path.
I can't test PyPDF2
on my end but the below should work for you.
from PyPDF2 import PdfFileReader
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
def get_info(path):
with open(path, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
author = info.author
creator = info.creator
producer = info.producer
subject = info.subject
title = info.title
print(number_of_pages, author, creator, producer, subject, title)
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
get_info(filename)
browsebutton = tk.Button(root, text="Browse", command=browsefunc)
browsebutton.pack()
pathlabel = tk.Label(root)
pathlabel.pack()
root.mainloop()
To address your question in the comments here is an example that displays in the GUI.
from PyPDF2 import PdfFileReader
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
label_list = []
def get_info(path):
with open(path, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
label_list[0].config(text=pdf.getNumPages())
label_list[1].config(text=info.author)
label_list[2].config(text=info.creator)
label_list[3].config(text=info.producer)
label_list[4].config(text=info.subject)
label_list[5].config(text=info.title)
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
get_info(filename)
browsebutton = tk.Button(root, text="Browse", command=browsefunc)
browsebutton.pack()
pathlabel = tk.Label(root)
pathlabel.pack()
for i in range(6):
label_list.append(tk.Label(root, text=""))
label_list[i].pack()
root.mainloop()
Upvotes: 1