用python裁剪PDF文档

在阅读PDF文档时,个人习惯在笔记本上双页阅读,但有的PDF边距太宽,看起来字很小,阅读吃力

本代码用来裁剪整个PDF文档间距

import PyPDF2
import os

left_margin = right_margin = 30
top_margin = 5
bottom_margin = 5

input_file_path = []
output_file_path = []
os.makedirs('.\\修改')  # 新建文件夹

file_path = '.\\'
file_list = os.listdir(file_path)
for i in file_list:
    if os.path.splitext(i)[1] == '.pdf':
        input_file_path.append('.\\' + i)
        output_file_path.append('.\\修改\\' + '修改' + i)


def split(page):
    page.mediaBox.lowerLeft = (left_margin, bottom_margin)
    page.mediaBox.lowerRight = (width - right_margin, bottom_margin)
    page.mediaBox.upperLeft = (left_margin, height - top_margin)
    page.mediaBox.upperRight = (width - right_margin, height - top_margin)


for m in range(len(input_file_path)):
    input_file = PyPDF2.PdfFileReader(open(input_file_path[m], 'rb'))
    output_file = PyPDF2.PdfFileWriter()

   page_info = input_file.getPage(0)
    width = float(page_info.mediaBox.getWidth())
    height = float(page_info.mediaBox.getHeight())
    page_count = input_file.getNumPages()

   for page_num in range(page_count):
        this_page = input_file.getPage(page_num)
        split(this_page)
        output_file.addPage(this_page)

   output_file.write(open(output_file_path[m], 'wb'))

使用之前
在这里插入图片描述
使用之后

在这里插入图片描述