Use Python to help the financial sister solve the PDF splitting. The sister said it was great...

Python learning and data mining 2021-10-28 16:29:49
use python help financial sister

The needs of the financial sister

Need from PDF Take a few pages out of the and save them as new PDF, Of course, because my little sister is a programming white , This tool needs to be made like a fool with GUI The form of the page
 picture

Select source pdf file , Then specify the new pdf File name and storage location , And what needs to be split page Information , You can get new pdf The file

Technology selection

about Python GUI, We have too many choices , Let's start with a simple horizontal comparison

At a high level , Big GUI The tools are :

  • Qt

  • WxWindows

  • Tkinter

  • Customer libraries(Kivy,Toga etc. )

  • Web relevant (HTML,Flask etc. )

But today , The tool I chose was appJar, It was invented by a great God engaged in Education , So it can provide a simpler GUI The creation process , And it's based entirely on Tkinter Of ,Python The default support

On and off !

First, in order to achieve PDF operation , I chose pypdf2 library

Let's hard code an example of input and output

from PyPDF2 import PdfFileWriter, PdfFileReader
infile = "Input.pdf"
outfile = "Output.pdf"
page_range = "1-2,6"

Next we instantiate PdfFileWriter and PdfFIleReader object , And create the actual Output.pdf file

output = PdfFileWriter()
input_pdf = PdfFileReader(open(infile, "rb"))
output_file = open(outfile, "wb")

The following complex point is the need to split pdf, Extract the page and save it in the list

page_ranges = (x.split("-") for x in page_range.split(","))
range_list = [i for r in page_ranges for i in range(int(r[0]), int(r[-1]) + 1)]

Finally, copy the content from the original file to the new file

for p in range_list:
output.addPage(input_pdf.getPage(p - 1))
output.write(output_file)

Let's build GUI Interface

For this split PDF Gadgets , The following functions are required :

  • You can select... Through the standard file browser pdf file

  • You can choose the location and file name of the output file

  • You can customize which pages to extract

  • There are some error checks

adopt PIP Install well appJar after , We can code

from appJar import gui
from PyPDF2 import PdfFileWriter, PdfFileReader
from pathlib import Path

establish GUI window

app = gui("PDF Splitter", useTtk=True)
app.setTtkTheme("default")
app.setSize(500, 200)

Here I use the default theme , Of course, you can also switch various theme modes

 picture

Here's how to add tags and data entry components

app.addLabel("Choose Source PDF File")
app.addFileEntry("Input_File")
app.addLabel("Select Output Directory")
app.addDirectoryEntry("Output_Directory")
app.addLabel("Output file name")
app.addEntry("Output_name")
app.addLabel("Page Ranges: 1,3,4-10")
app.addEntry("Page_Ranges")

Next, add a button ,“ Handle ” and “ sign out ”, Press the button , Call the following functions

app.addButtons(["Process", "Quit"], press)

Finally, run this app La

# start the GUI
app.go()

We'll finish it GUI Build , Write the internal processing logic . The program reads any input , Judge whether it is PDF, And split

def press(button):
if button == "Process":
src_file = app.getEntry("Input_File")
dest_dir = app.getEntry("Output_Directory")
page_range = app.getEntry("Page_Ranges")
out_file = app.getEntry("Output_name")
errors, error_msg = validate_inputs(src_file, dest_dir, page_range, out_file)
if errors:
app.errorBox("Error", "\n".join(error_msg), parent=None)
else:
split_pages(src_file, page_range, Path(dest_dir, out_file))
else:
app.stop()

If you click “ Handle (Process)” Button , Call app.getEntry() Retrieve input values , Each value is stored , And then by calling validate_inputs() To verify

Let's see validate_inputs function

def validate_inputs(input_file, output_dir, range, file_name):
errors = False
error_msgs = []
# Make sure a PDF is selected
if Path(input_file).suffix.upper() != ".PDF":
errors = True
error_msgs.append("Please select a PDF input file")
# Make sure a range is selected
if len(range) < 1:
errors = True
error_msgs.append("Please enter a valid page range")
# Check for a valid directory
if not(Path(output_dir)).exists():
errors = True
error_msgs.append("Please Select a valid output directory")
# Check for a file name
if len(file_name) < 1:
errors = True
error_msgs.append("Please enter a file name")
return(errors, error_msgs)

This function just performs some checks to ensure that the input data is valid

After collecting and verifying all the data , You can call split Function to process files

def split_pages(input_file, page_range, out_file):
output = PdfFileWriter()
input_pdf = PdfFileReader(open(input_file, "rb"))
output_file = open(out_file, "wb")
page_ranges = (x.split("-") for x in page_range.split(","))
range_list = [i for r in page_ranges for i in range(int(r[0]), int(r[-1]) + 1)]
for p in range_list:
# Need to subtract 1 because pages are 0 indexed
try:
output.addPage(input_pdf.getPage(p - 1))
except IndexError:
# Alert the user and stop adding pages
app.infoBox("Info", "Range exceeded number of pages in input.\nFile will still be saved.")
break
output.write(output_file)
if(app.questionBox("File Save", "Output PDF saved. Do you want to quit?")):
app.stop()

Okay , This requires us to complete a simple GUI Split PDF File tools .

Reference link :https://mp.weixin.qq.com/s/_dQrXzn9cOUoA6jojt0h-w


Technical communication

Welcome to reprint 、 Collection 、 Gain some praise and support !

 Insert picture description here

At present, a technical exchange group has been opened , Group friends have exceeded 2000 people , The best way to add notes is : source + Interest direction , Easy to find like-minded friends

  • The way ①、 Send the following picture to wechat , Long press recognition , The background to reply : Add group ;
  • The way ②、 Add microsignals :dkl88191, remarks : come from CSDN
  • The way ③、 WeChat search official account :Python Learning and data mining , The background to reply : Add group

 Long press attention

版权声明
本文为[Python learning and data mining]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211012224418242A.html

  1. Python Qt GUI设计:QPrinter打印图片类(基础篇—21)
  2. python中一维数组怎么转置
  3. 遇到一个问题,关于Python的复数的
  4. python批量将文件夹里的csv文件转成txt文件
  5. Python 彻底搞懂列表推导式|初级到进阶(实例30+)
  6. python爬虫,遇到urllib.error.URLError: &lt;urlopen error [Errno 11001] getaddrinfo failed&gt;错误
  7. Python程序设计输入五到十之间的整数n,生成并输出一个n位的验证码,不能把大小写字母和数字全写出来
  8. python程序打包之后没运行tkinter
  9. python提取多个Excel文件内目的格内容按一定格式写入新的Excel。
  10. pandas读取csv文件文件末尾都是NaN值,发生了什么?
  11. Python使用matplotlib可视化自定义背景色实战:自定义可视化图像的背景色(Background Color)
  12. 大学生Python实验的列表与元组
  13. python 关于线程的参数args的问题
  14. 如何用python将pdf转成网页html的格式
  15. python基础题 运行后卡在某一步 随后电脑疯狂运转发声 等待无果
  16. 求大神教怎么用python写字
  17. 云计算开发:Python3-expandtabs()方法详解
  18. 9000字深度整理: 全网最详细 Pandas 合并数据集操作总结!
  19. python这个代码有错误,请帮我修改
  20. python redis自带门神 lock 方法
  21. 有关python求众数,中位数和均值的题目
  22. 零基础5天入门Python数据分析:第四课
  23. 零基础5天入门Python数据分析:第三课(上)
  24. 零基础5天入门Python数据分析:第一课
  25. python redis自带门神 lock 方法
  26. 【算法学习】LCP 01. 猜数字(java / c / c++ / python / go)
  27. 【Python量化分析100例】Day2-星期几最容易被割韭菜
  28. python逆推年份,前两问写好了,第三问不会
  29. Python 爬取百度网页如何绕过安全验证
  30. 零基础5天入门Python数据分析:第五课
  31. Python人脸融合时出现关于pybind11的问题
  32. python如何返回除数,公约数,倍数
  33. python 返回多重嵌套列表(多于两层嵌套)的元素
  34. 用Python采集了几千条相亲文案,终于发现了告别单身的秘密
  35. python正负序列题,目前只学到循环,怎么做啊(*꒦ິ⌓꒦ີ)
  36. 拿爱奇艺练手Python爬虫,是在法律边缘试探吗?爬虫技巧学习
  37. Python注释删除代码依然报错
  38. python的pyautogui模块中的pyautogui.scroll()括号中无论写什么值滚动范围都相同
  39. 为什么python在vscode里运行报语法错误,在IDLE里就不会
  40. 请问python如何在将pdf转成word时,去除pdf上的页眉页脚(或者对于每页pdf只取第2行-倒数第二行)
  41. matlab改为python,偏最小二乘回归分析的一个程序
  42. 应该是python基础题希望能用基础方法解决
  43. 想找个会Python的做场外援助,上课没听明白
  44. Python程序,插入不了MySQL的date格式
  45. (初学者)关于Python操作Excel问题
  46. 求人来解答这两道Python题
  47. python中用三引号换行,举例说明
  48. python数码管该怎么用,十四段
  49. python进行中文文本聚类(切词以及Kmeans聚类)
  50. Python - 转换二进制为ASCII码
  51. Python - 在段落中计算令牌
  52. Python - 排序线
  53. Python - 字符串不变性
  54. Python - 文本摘要
  55. Python+微信小程序开发(六)双向绑定和前后端通信
  56. 基于Anaconda搭建Django环境
  57. Django基础篇(2)--视图
  58. 288页的python编程文档,从入门到实践,入门看这一篇就够了
  59. Python Web实战:Flask + Vue 开发一个漂亮的词云网站
  60. 让我深夜十二点催她睡觉,我用 Python 轻松搞定!