使用 Python Tkinter 构建 GUI Pdf Extract 应用程序

50 篇文章 37 订阅 ¥39.90 ¥99.00
本文介绍了如何使用Python的Tkinter库构建一个GUI应用程序,该应用能实现PDF文本的提取。通过讲解Tkinter的基础知识,包括Widgets、Geometry Management等,以及PyPDF2库的使用,读者将学会如何创建一个具有打开、编辑和保存PDF文本功能的界面。
摘要由CSDN通过智能技术生成

一、什么是Tkinter?

根据维基百科,Tkinter 是一个绑定到 Tk GUI 工具包的 Python。Tkinter 提供了一种快速简单的方法来构建图形用户界面。Tkinter 为 Tk GUI 工具包提供了一个强大的面向对象的接口。

在本文中,我们将使用 Tkinter 构建一个 Python GUI pdf 提取应用程序。本博客将向您介绍 Tkinter,到最后,您应该能够构建一个 pdf 提取应用程序。

二、Widgets小部件

  • 小部件通常是图形用户界面 (GUI) 的一个组件。
  • “根”窗口是唯一的例外,它是将包含所有其他内容且没有父窗口的顶级窗口。

三、小部件类

tkinter 中内置了许多不同的小部件类,但我们在这里使用了一些小部件:

  • Label = 用于在屏幕上显示文本或图像。
  • Button = 用于向您的应用程序添加按钮。
  • Canvas = 用于绘制图片和其他布局,如文本、图形等。
  • Frame = 它用作容纳和组织小部件的容器。
  • Text = 它允许用户编辑多行文本并按照显示方式设置格式。

四、Geometry Management</

Abstract Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 14.2. Top-level menus.........................................................................................................56 15. The Menubutton widget......................................................................................................57 16. The Message widget............................................................................................................59 17. The OptionMenu widget.......................................................................................................60 18. The PanedWindow widget....................................................................................................61 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................64 20. The Scale widget................................................................................................................67 21. The Scrollbar widget........................................................................................................70 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................78 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................83 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................84 24. Toplevel: Top-level window methods..................................................................................91 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................101 26.1. How to name a widget class......................................................................................102 26.2. How to name a widget instance.................................................................................102 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................104 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 New Mexico Tech Computer Center Tkinter reference 2 30. Events................................................................................................................................107 30.1. Levels of binding......................................................................................................108 30.2. Event sequences.......................................................................................................109 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................111 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................115 30.8. Virtual events...........................................................................................................116 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................118 31.3. The tkColorChooser module.................................................................................119
Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 15. The Menubutton widget......................................................................................................56 16. The Message widget............................................................................................................58 17. The OptionMenu widget.......................................................................................................59 18. The PanedWindow widget....................................................................................................60 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................63 20. The Scale widget................................................................................................................66 21. The Scrollbar widget........................................................................................................69 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................77 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................82 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................83 24. Toplevel: Top-level window methods..................................................................................90 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................100 26.1. How to name a widget class......................................................................................101 26.2. How to name a widget instance.................................................................................101 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................103 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 30. Events................................................................................................................................107 New Mexico Tech Computer Center Tkinter reference 2 30.1. Levels of binding......................................................................................................107 30.2. Event sequences.......................................................................................................108 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................110 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................114 30.8. Virtual events...........................................................................................................115 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................117 31.3. The tkColorChooser module.................................................................................118
要实现一个带有GUI界面的OCR识别工具,可以使用PythonTkinter库来构建用户界面,使用之前提到的代码来实现OCR识别和Excel保存功能。 下面是一个示例代码,实现将GUI界面与OCR识别和Excel保存功能结合起来: ```python import os import pytesseract import pandas as pd from pdfminer.high_level import extract_text import tkinter as tk from tkinter import filedialog # 创建GUI界面 root = tk.Tk() root.title('PDF OCR识别工具') root.geometry('400x300') # 创建文件选择按钮 def choose_file(): file_path = filedialog.askopenfilename() file_path_entry.delete(0, tk.END) file_path_entry.insert(0, file_path) file_path_label = tk.Label(root, text='选择PDF文件:') file_path_label.pack() file_path_entry = tk.Entry(root) file_path_entry.pack() file_choose_button = tk.Button(root, text='选择文件', command=choose_file) file_choose_button.pack() # 创建OCR识别按钮 def ocr(): pdf_path = file_path_entry.get() text = extract_text(pdf_path) result = pytesseract.image_to_string(text) df = pd.DataFrame({'text': [result]}) df.to_excel('result.xlsx', index=False) result_label.config(text='OCR识别完成!') ocr_button = tk.Button(root, text='OCR识别', command=ocr) ocr_button.pack() # 创建结果显示标签 result_label = tk.Label(root, text='') result_label.pack() root.mainloop() ``` 在这个示例代码中,我们创建了一个GUI界面,包括一个文件选择按钮、一个OCR识别按钮和一个结果显示标签。当用户点击文件选择按钮时,会弹出文件选择对话框,用户可以选择要识别的PDF文件。当用户点击OCR识别按钮时,程序会读取文件路径,进行OCR识别,并将结果保存到Excel中,同时在结果显示标签中显示“OCR识别完成!”的提示信息。 需要注意的是,这个示例代码只能识别单个PDF文件,如果要实现批量OCR识别,需要对代码进行修改。可以使用Python的os库来遍历文件夹中的PDF文件,对每个文件进行OCR识别和Excel保存操作。同时,可以使用Python的多线程或者多进程来加速处理速度,提高用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值