首页 > 编程语言 >分享一个python写的pdf拆分合并小工具

分享一个python写的pdf拆分合并小工具

时间:2023-11-26 10:01:28浏览次数:35  
标签:file python self 拆分 path pdf wx size

分享一个python写的pdf拆分合并小工具_起始页

github 地址https://github.com/Biexei/pdf-tools

1.安装 requirements.txt 中的库文件pip install -r requirements.txt

2.打包成 exePyinstaller -F -w pdf.py

import wximport osfrom PyPDF2 import PdfFileReader, PdfFileWriterimport time


def pdf_merge(out_put_path: str, *input_files) -> None:

    """

合并pdf

    :param out_put_path: 合并结果输出路径

    :param input_files: 待合并pdf文件

    :return: None

    """

    pdf_writer = PdfFileWriter()

    out_path = out_put_path + r"\合并结果%s.pdf" % str(int(time.time()))

    for file in input_files:

        reader = PdfFileReader(open(file=file, mode='rb'))

        page_size = reader.getNumPages()

        for i in range(page_size):

            pdf_writer.addPage(reader.getPage(i))

    with open(file=out_path, mode='wb'):

        pdf_writer.write(out_path)


def pdf_split(in_put_file: str, size_range: str, out_put_path: str) -> None:

    """

拆分pdf

    :param in_put_file: 被拆分pdf文件

    :param size_range: 起始页(包含)-截止页(包含),如1-3

    :param out_put_path: 拆分结果输出目录

    :return: None

    """

    rg = size_range.split(",")

    for _ in rg:

        range_split = _.split("-")

        start = int(range_split[0])

        end = int(range_split[1])

        writer = PdfFileWriter()

        reader = PdfFileReader(open(file=in_put_file, mode='rb'))

        page_size = reader.getNumPages()

        if start < 1:

            raise Exception("起始页参数错误")

        if start > end:

            raise Exception("参数错误")

        if end > page_size:

            raise Exception("截止页参数错误,超出最大页码数:%s" % str(page_size))

        for i in range(start - 1, end):

            writer.addPage(reader.getPage(i))

        path = out_put_path + r"\\%s-%s.pdf" % (start, end)

        with open(file=path, mode='wb'):

            writer.write(path)


class SiteLog(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title='PDF工具', size=(640, 480))

        # PDF合并        self.SelBtn = wx.Button(self, label='PDF合并', pos=(5, 5), size=(70, 70))

        self.file_name_text_1 = wx.StaticText(self, label='文件1', pos=(80, 5), size=(50, 25))

        self.file_path_1 = wx.TextCtrl(self, pos=(140, 5), size=(230, 25))

        self.choose_btn_1 = wx.Button(self, label='选择文件1', pos=(400, 5), size=(80, 25))

        self.choose_btn_1.Bind(wx.EVT_BUTTON, self.on_open_file1)

        self.file_name_text_2 = wx.StaticText(self, label='文件2', pos=(80, 50), size=(50, 25))

        self.file_path_2 = wx.TextCtrl(self, pos=(140, 50), size=(230, 25))

        self.choose_btn_2 = wx.Button(self, label='选择文件2', pos=(400, 50), size=(80, 25))

        self.choose_btn_2.Bind(wx.EVT_BUTTON, self.on_open_file2)

        self.merge_btn = wx.Button(self, label='合并', pos=(520, 5), size=(70, 70))

        self.merge_btn.Bind(wx.EVT_BUTTON, self.on_merge)


        # PDF拆分        self.SelBtn = wx.Button(self, label='PDF拆分', pos=(5, 200), size=(70, 70))

        self.file_name_text = wx.StaticText(self, label='文件', pos=(80, 200), size=(50, 25))

        self.file_path = wx.TextCtrl(self, pos=(140, 200), size=(230, 25))

        self.choose_btn = wx.Button(self, label='选择文件', pos=(400, 200), size=(80, 25))

        self.choose_btn.Bind(wx.EVT_BUTTON, self.on_open_file)

        self.size_range = wx.StaticText(self, label='区间', pos=(80, 245), size=(50, 25))

        self.size_range_value = wx.TextCtrl(self, pos=(140, 245), size=(230, 25))

        self.size_range_value.SetHint("如1-4,多个区间如1-3,3-4")

        self.split_btn = wx.Button(self, label='拆分', pos=(520, 200), size=(70, 70))

        self.split_btn.Bind(wx.EVT_BUTTON, self.on_split)


    def on_open_file1(self, event):

        """

        PDF合并文件1选择事件

        :param event:

        :return:

        """

        wildcard = 'Allfiles(*.*)|*.*'

        dialog = wx.FileDialog(None, 'select', os.getcwd(), '', wildcard, wx.FC_OPEN)

        if dialog.ShowModal() == wx.ID_OK:

            self.file_path_1.SetValue(dialog.GetPath())

            dialog.Destroy()


    def on_open_file2(self, event):

        """

        PDF合并文件2选择事件

        :param event:

        :return:

        """

        wildcard = 'Allfiles(*.*)|*.*'

        dialog = wx.FileDialog(None, 'select', os.getcwd(), '', wildcard, wx.FC_OPEN)

        if dialog.ShowModal() == wx.ID_OK:

            self.file_path_2.SetValue(dialog.GetPath())

            dialog.Destroy()


    def on_open_file(self, event):

        """

        PDF拆分文件选择事件

        :param event:

        :return:

        """

        wildcard = 'Allfiles(*.*)|*.*'

        dialog = wx.FileDialog(None, 'select', os.getcwd(), '', wildcard, wx.FC_OPEN)

        if dialog.ShowModal() == wx.ID_OK:

            self.file_path.SetValue(dialog.GetPath())

            dialog.Destroy()


    def on_merge(self, event):

        """

        PDF合并事件

        :param event:

        :return:

        """

        crt_path = os.getcwd()

        pdf_merge(crt_path, self.file_path_1.GetValue(), self.file_path_2.GetValue())

        toast = wx.MessageDialog(None, "合并成功")

        if toast.ShowModal() == wx.ID_YES:

            toast.Destroy()


    def on_split(self, event):

        """

        PDF拆分事件

        :param event:

        :return:

        """

        toast = "拆分成功"

        crt_path = os.getcwd()

        try:

            pdf_split(self.file_path.GetValue(), self.size_range_value.GetValue(), crt_path)

        except Exception as e:

            toast = str(e)

        toast = wx.MessageDialog(None, toast)

        if toast.ShowModal() == wx.ID_YES:

            toast.Destroy()


if __name__ == '__main__':

    app = wx.App()

    SiteFrame = SiteLog()

    SiteFrame.Center()

    SiteFrame.Show()

    app.MainLoop()


标签:file,python,self,拆分,path,pdf,wx,size
From: https://blog.51cto.com/u_16350833/8566393

相关文章

  • python工具集
    selenium介绍:Selenium是一种开源工具,用于在Web浏览器上执行自动化测试。背景:对已推送到搜索引擎的url进行下线(用户反馈不想展现)。版本:python3.8.9,selenium4.15.2。fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByimportreimporttimeq......
  • 【专题】2023快手母婴行业数据报告PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=33866原文出处:拓端数据部落公众号品牌一直在思考如何更好地了解消费者的需求,特别是在年轻化和线上消费趋势加强的母婴行业。根据《2023母婴行业数据报告合集》,短视频直播平台成为该行业新的增长点。报告合集显示,母婴商品的消费人数在2022年全年和2......
  • 聪明办法学python Task2
    **数据类型操作**对于常用内置库python对于编译有很多的封装函数python是一门面向对象友好的语言分类与回归回归比方说游戏本均价8K这是回归反之分类是8K能买到什么东......
  • 【专题】展望人工智能银行:当银行遇到AI报告PDF合集分享(附原数据表)
    报告链接:http://tecdat.cn/?p=32210在2016年,AlphaGo机器人打败了18届世界棋王李世石,成为了世界棋坛上最伟大的人物。阅读原文,获取专题报告全文,解锁154份文末人工智能银行相关报告。围棋是一种非常复杂的棋类,它要求有很强的直觉,想像力和策略性的思考,而这一切在很长一段时间里都......
  • PYTHON用KERAS的LSTM神经网络进行时间序列预测天然气价格例子|附代码数据
    全文下载链接:http://tecdat.cn?p=26519最近我们被客户要求撰写关于LSTM的研究报告,包括一些图形和统计输出。一个简单的编码器-解码器LSTM神经网络应用于时间序列预测问题:预测天然气价格,预测范围为10天。“进入”时间步长也设置为10天。)只需要10天来推断接下来的10天。......
  • 聪明办法学python(2)
    聪明办法学python(2)TASK03:数据类型与操作一.常用内置类型:1.1整数integer(int)1.2浮点数Floatprint(0.1+0.1==0.2)#Trueprint(0.1+0.1+0.1==0.3)#False!print(0.1+0.1+0.1)#0.30000000000000004print((0.1+0.1+0.1)-0.3)#特别小,5.55111512312......
  • Python2
    Python笔记2常用内置类型整数int浮点数Float### 布尔值boolTrue——>真False——>假类型Typetype()返回类型内置常数math中的常量pietauinf浮点正无穷大常用内置运算符算数+-*/,浮点除5/2=2.5//,整除5//2=2%,取余5%2=1**,......
  • 聪明办法学python-task2
    变量与函数变量-->标签;新值数据类型和旧值不必相同,覆盖变量命名规则必须以字母下划线开头,命名由字母,数字,下划线组成大小写敏感尽量避免使用保留字命名多变量赋值a,b,c=1,2,6两边对等,元组的解包函数headerbody在Python中,定义一个函数要使用def语句,依次写出函......
  • 聪明办法学python task03,task04
    条件语句  if条件:                      多个条件判断用elif如果判断需要多个条件需同时判断时,可以使用or(或),表示两个条件有一个成立时判断条件成功;使用and(与)时,表示只有两个条件同时成立的情况下,判断条件才成功循环语句:for......
  • python第二次学习笔记
    python中的四大数据类型:1.list(列表):*list是一种有序的集合,可以随时添加和删除其中的元素。比如,我们可以列出一些名字就可以用一个list表示:  >>>names=['Mike','Tom','Bob']我们也可以列出一些数字:>>>numbers=[1,3,5,6,7]在第一个的列表中,我们......