关于程序员:Python自动化办公之PDF拆分

2次阅读

共计 3416 个字符,预计需要花费 9 分钟才能阅读完成。

明天咱们持续分享实在的自动化办公案例,心愿各位 Python 爱好者可能从中失去些许启发,在本人的工作生存中更多的利用 Python,使得工作事倍功半!

需要

须要从 PDF 中取出几页并将其保留为新的 PDF,为了前期使用方便,这个工具须要做成傻瓜式的带有 GUI 页面的模式

抉择源 pdf 文件,再指定下生成的新的 pdf 文件名称及保留地位,和须要拆分的 page 信息,就能够失去新的 pdf 文件了

需要解析

对于 Python GUI,咱们有太多种抉择了,上面咱们先来横向的简略比照下

从高层次上看,大的 GUI 工具有:

  • Qt
  • WxWindows
  • Tkinter
  • Customer libraries(Kivy,Toga 等)
  • Web 相干(HTML,Flask 等)

不过明天,咱们抉择的工具是 appJar,这是一个由一位从事教育工作的大神创造的,所以它能够提供一个更加简略的 GUI 创立过程,而且是齐全基于 Tkinter 的,Python 默认反对

代码实现

首先为了实现 PDF 操作,我这里抉择了 pypdf2 库

咱们先硬编码一个输入输出的示例

from PyPDF2 import PdfFileWriter, PdfFileReader

infile = "Input.pdf"
outfile = "Output.pdf"

page_range = "1-2,6"

接下来咱们实例化 PdfFileWriter 和 PdfFIleReader 对象,并创立理论的 Output.pdf 文件


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

上面一个比较复杂的点就是须要拆分 pdf,提取页面并保留在列表中

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:
    output.addPage(input_pdf.getPage(p - 1))
output.write(output_file)

上面来构建 GUI 界面

对于这个拆分 PDF 的小工具,须要具备如下性能:

  • 能够通过标准文件浏览器抉择 pdf 文件
  • 能够抉择输入文件的地位及文件名称
  • 能够自定义提取哪些页面
  • 有一些谬误查看

通过 PIP 装置好 appJar 后,咱们就能够编码了

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

创立 GUI 窗口

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

这里我应用了默认主题,当然也能够切换各种各样的主题模式

上面是增加标签和数据输出组件

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

接下来增加按钮,“解决”和“退出”,按下按钮,调用如下函数

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

最初就是运行这个 app 啦

# start the GUI
app.go()

这要咱们就实现了 GUI 的搭建,上面编写外部解决逻辑。程序读取任何输出,判断是否为 PDF,并拆分

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

如果单击“解决(Process)”按钮,则调用 app.getEntry() 检索输出值,每个值都会被存储,而后通过调用 validate_inputs() 进行验证

来看看 validate_inputs 函数

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)

这个函数就是执行一些查看来确保输出有数据并且无效

在收集验证了所以数据后,就能够调用 split 函数来解决文件了

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

好了,这样咱们就实现了一个繁难的 GUI 拆分 PDF 文件的工具喽

喜爱就在看、点赞,转发,三连反对一下噻!

本文由 mdnice 多平台公布

正文完
 0