我如何使用tkinter和PdFileReader从文件路径获取数据

时间:2018-07-31 07:07:06

标签: python-3.x tkinter

我使用这种方法:

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()

但是,出现以下错误:

  

TypeError:无效文件:无

如何从pathlabel获取文件?

是否可以从文件路径打开文件?

1 个答案:

答案 0 :(得分:1)

我在这里看到几个问题。

删除此:

if __name__ == '__main__':
    path = pathlabel.pack()
    get_info(path)

这是在使path = pathlabel.pack()的结果。该结果将始终为None,因为pack()总是要返回None。然后,您将None传递给get_info()函数。

相反,让您从get_info函数中调用brousefunc()

brousefunc()更改为此:

def browsefunc():
    filename = filedialog.askopenfilename()
    pathlabel.config(text=filename)
    get_info(filename)

这将更新您的标签,然后还将正确的文件路径发送到get_info函数。

如果您仍然希望在文件路径中使用标签的文本,则可以执行path = pathlabel["text"],因为这会将文本的值分配给该路径。

我无法测试PyPDF2,但以下内容对您有用。

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()

要在注释中解决您的问题,下面是一个显示在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()