吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2455|回复: 26
收起左侧

[Python 原创] pdf递归合并小工具(含层次目录)

[复制链接]
xxmdmst 发表于 2023-9-22 10:36
合并效果:
934b1f8cdc1fb1de0df2bcf12c165b6d_d594e61023bcdeefd3c5e2b4013bab61.png
99a71cc90cc65e1a65896a8662a2c851_574aaf41e9e25868e3bab9defec46cb1.png
针对新版PyPDF2 更新的代码为:
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from PIL import Image, ImageFont, ImageDraw
import re
import os
from PyPDF2 import PdfReader, PdfWriter
 
 
class PDFMerge:
    "作者:小小明  博客地址:https://xxmdmst.blog.csdn.net/"
 
    @staticmethod
    def get_mark_img(text, size=30):
        width = len(text) * size
        mark = Image.new(mode='RGB', size=(
            width, size + 20), color=(255, 255, 255))
        ImageDraw.Draw(im=mark) \
            .text(xy=(0, 0),
                  text=text,
                  fill="black",
                  font=ImageFont.truetype('msyhbd.ttc', size=size))
        mark.save("watermark.pdf", "PDF", resolution=100.0, save_all=True)
        return mark
 
    @staticmethod
    def windows_files_sort(files):
        files.sort(key=lambda s: [(s, int(n))
                                  for s, n in re.findall('(\D+)(\d+)', f'a{s}0')])
 
    def __merge_pdf_in(self, path, parent=None):
        files = []
        dirs = []
        for file in os.listdir(path):
            file = os.path.join(path, file)
            if os.path.isfile(file):
                if file.endswith(".pdf"):
                    files.append(file)
            elif os.path.isdir(file):
                if os.path.basename(file) != "__MACOSX":
                    dirs.append(file)
        PDFMerge.windows_files_sort(files)
        PDFMerge.windows_files_sort(dirs)
 
        for pdf_file in files:
            pdf_reader = PdfReader(pdf_file)
            pdf_file = os.path.basename(pdf_file)
            pageCount = len(pdf_reader.pages)
            print(pdf_file, pageCount, self.pagenum_total)
            for page in pdf_reader.pages:
                page.compress_content_streams()
                self.pdf_writer.add_page(page)
            self.pdf_writer.add_outline_item(
                pdf_file[:pdf_file.rfind(".")], self.pagenum_total, parent=parent)
            self.pagenum_total += pageCount
        for path in dirs:
            title = os.path.basename(path)
            print(title, self.pagenum_total)
            PDFMerge.get_mark_img(title)
            watermark = PdfReader('watermark.pdf').pages[0]
            self.pdf_writer.add_page(watermark)
            os.remove('watermark.pdf')
            parent_id = self.pdf_writer.add_outline_item(
                title, self.pagenum_total, parent=parent)
            self.pagenum_total += 1
            self.__merge_pdf_in(path, parent=parent_id)
 
    def merge_pdf(self, path, out_name):
        self.pagenum_total = 0
        self.pdf_writer = PdfWriter()
        self.__merge_pdf_in(path)
        # os.remove('watermark.pdf')
        print("总页数:", self.pagenum_total)
        print("开始写出到文件")
        with open(out_name, "wb") as outputfile:
            self.pdf_writer.write(outputfile)
        print("PDF文件合并完成")
 
 
if __name__ == '__main__':
    pdfmerge = PDFMerge()
    pdfmerge.merge_pdf(r"D:\tmpVoice\pdf", "test.pdf")


现成工具下载地址:https://gitcode.net/as604049322/python_gui/-/raw/master/pdf_tools_v0.2.exe?inline=false

原贴:https://blog.csdn.net/as604049322/article/details/126647906

免费评分

参与人数 8吾爱币 +9 热心值 +8 收起 理由
wutljs + 1 + 1 热心回复!
dhwl9899 + 1 + 1 谢谢@Thanks!
jyzj + 1 + 1 谢谢@Thanks!
junjia215 + 1 + 1 谢谢@Thanks!
huiks + 1 + 1 我很赞同!
hedun + 1 + 1 我很赞同!
52bojie + 1 + 1 谢谢@Thanks!
山岚 + 2 + 1 用心讨论,共获提升!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| xxmdmst 发表于 2023-9-23 08:35
52bojie 发表于 2023-9-22 20:40
哈哈哈,谢谢大佬

本次打包使用了upx,https://upx.github.io/
只需要将upx加入环境变量,pyinstaller打包时便会自动使用

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
hedun + 1 + 1 热心回复!

查看全部评分

尛航 发表于 2023-9-22 11:35
xiaotwins 发表于 2023-9-22 12:50
Atnil 发表于 2023-9-22 14:07
xiaotwins 发表于 2023-9-22 12:50
大佬牛逼呀 代码量这么少 功能缺很多

哈哈哈 哥啊 您这是夸他还是调侃他 缺还是不缺
52bojie 发表于 2023-9-22 14:16
Atnil 发表于 2023-9-22 14:07
哈哈哈 哥啊 您这是夸他还是调侃他 缺还是不缺

是却还缺,哈哈哈。。。。。。

免费评分

参与人数 1吾爱币 +1 收起 理由
Atnil + 1 热心回复!

查看全部评分

52bojie 发表于 2023-9-22 14:17
拿来运行试试,有咩有惊喜来着
52bojie 发表于 2023-9-22 14:23
楼主,exe是用什么GUI生成的呀
xiaotwins 发表于 2023-9-22 14:42
Atnil 发表于 2023-9-22 14:07
哈哈哈 哥啊 您这是夸他还是调侃他 缺还是不缺

写错了 应该是代码量不少 功能却很多

免费评分

参与人数 1热心值 +1 收起 理由
Atnil + 1 热心回复!

查看全部评分

zhangj005508 发表于 2023-9-22 15:41
学习中,谢谢分享
 楼主| xxmdmst 发表于 2023-9-22 18:08
52bojie 发表于 2023-9-22 14:23
楼主,exe是用什么GUI生成的呀

你是问gui用的Python的什么库吗?pyinstaller
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-23 18:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表