python使用pypdf2库实现多页pdf为一组放到新的A4大小的一页pdf中,并添加横线

1 设计思路

1.1 把pdf四页为一组,合成一页插入到新pdf左侧

  • step1——引入PyPDF2库,和设置一些调节因子和页面初始位置
  • step2——打开输入的PDF文件,并使用PdfFileReader函数读取文件。同时创建一个PdfFileWriter对象,以便于写入拼接后的新PDF文件。
  • step3——对于输入PDF的每4页,执行以下操作:
    • 创建一个新的PDF页面,大小为A4,(A4是国际标准纸张规格之一,尺寸为210毫米 × 297毫米,换算为英寸为8.27英寸 × 11.69英寸。在制作PDF文件时,通常使用点(pt)作为单位,因此A4纸的尺寸为595pt × 842pt。)将它添加到PdfFileWriter对象中
    • 计算当前4页中最大的高度,并根据它来调整每一页的大小
    • 遍历这4页并将它们缩小并添加到新的PDF页面的左侧
  • step 4——将新的PDF文件保存在输出文件中

1.2 把横线插入到pdf右侧

直接把横线插入pdf查询了很多方法,都不太理想。后面想到的思路是直接把横线转成pdf文件,同样以pdf的形式插入到新的pdf页面中。

1.3 在pdf右上角插入条码

首先在百度找一个条码生成器,生成自己的条码。

然后与插入横线类型类似,转成pdf插入到新的pdf页面中。

2 结果图

image

3 全部代码如下

"""
教师每次下发课件是PDF格式,由若干页(N页)横版的PPT组成,例如:
这样的格式不方便做笔记,于是你希望完成一个软件,将上述PDF文件转成下面的形式的PDF,共计N/4页(如不能整除则向上取整),每页的效果如下:
即每页(A4版面)左侧放置4版的课件(若不足4版则下部为空白),右侧放置多条记笔记的横线,右上方为条码,内容为自己的学号(略去学号中的字母,不足补零)。
完成上述代码功能,同时完成j较为详细的软件设计文档(最终提交pdf即可),文档包括设计思路、关键代码解释、效果图等。
"""
"""
A4是国际标准纸张规格之一,尺寸为210毫米 × 297毫米,换算为英寸为8.27英寸 × 11.69英寸。在制作PDF文件时,通常使用点(pt)作为单位,
因此A4纸的尺寸为595pt × 842pt。
"""

import PyPDF2

max_height_a = 100  # 调节因子,因为有些PPT空隙太多了不好看,但算max_height高时会算进去
cur_y_a = 50  # 也是调节因子,调节pdf距离顶端的范围

# 打开PDF文件并读取页面
with open('input.pdf', 'rb') as file:
    reader = PyPDF2.PdfFileReader(file, strict=False)
    writer = PyPDF2.PdfFileWriter()

    # 遍历每一页,每4页为一组进行处理
    for i in range(0, reader.getNumPages(), 4):
        # 设置新页面的大小为A4
        page_width = 595
        page_height = 842
        # 创建新的一页并调整页面大小
        page = writer.addBlankPage(width=page_width, height=page_height)


        # 设置页面初始位置,左上角
        cur_y = page_height - cur_y_a
        # 计算当前组中最大的高度
        max_height = 0
        for j in range(4):
            if i+j < reader.getNumPages():
                cur_page = reader.getPage(i+j)
                max_height = max(max_height, cur_page.mediaBox.getHeight())


        # 遍历4页并将其缩小并添加到新的一页的左侧
        for j in range(4):
            if i+j < reader.getNumPages():
                cur_page = reader.getPage(i+j)
                cur_page_height = page_height/4
                """插入Pdf页面,4页1组"""
                # 计算缩放比例,可以根据调节因子调节下
                scale = cur_page_height / (float(max_height) - max_height_a)
                cur_page.scale_by(factor=scale)

                # 将当前页面添加到新页面,并进行位置调整
                page.mergeTranslatedPage(cur_page, 0, cur_y, expand=True)

                """插入横线pdf"""
                line_pdf_file = open('line.pdf', 'rb')
                reader_line = PyPDF2.PdfFileReader(line_pdf_file, strict=False)
                line_page = reader_line.getPage(0)
                line_page.scale_by(factor=0.4)
                page.mergeTranslatedPage(line_page, float(cur_page.mediaBox.getWidth()), cur_y+20, expand=True)

                # 更新当前位置
                cur_y = cur_y - cur_page.mediaBox.getHeight()

        """最后插入条形码"""
        ycx_pdf_file = open('ycx.pdf', 'rb')
        reader_ycx = PyPDF2.PdfFileReader(ycx_pdf_file, strict=False)
        ycx_page = reader_ycx.getPage(0)
        ycx_page.scale_by(factor=0.08)
        page.mergeTranslatedPage(ycx_page, page_width+35,
                                 page_height + 155, expand=True)

    # 保存新的PDF文件
    with open('output.pdf', 'wb') as output:
        writer.write(output)



posted @ 2023-04-17 21:21  JaxonYe  阅读(513)  评论(0编辑  收藏  举报