-
修改页面尺寸
import os
import shutil
import fitz # PyMuPDFdef cm_to_px(cm):
# 厘米转换成像素
"""
doc = fitz.open(input_file)
page0 = doc[0]
width_px = page0.mediabox.width
height = page0.mediabox.height
print(f'width_px:{width_px} height:{height}')
"""
import math
# DPI 计算, 也可以通过计算得到
# width_cm = 10 # cm # wps office 页面属性显示的尺寸
# width_px = 284.0 # px # 程序读取的页面像素尺寸
# DPI = int((width_px * 2.54) / width_cm) # 每英寸的像素数
# print('DPI:', DPI)DPI = 72 # 72 DPI:通常用于网页图像,适合显示器上的低分辨率图像 calculated_height_px = math.ceil((cm * DPI) / 2.54) print('calculated_height_px:', calculated_height_px) # print(cm * 72 / 2.54) return calculated_height_pxdef resize_pdf_pages(input_file, output_file, target_width=15, target_height=20):
"""
:param input_file:
:param output_file:
:param target_width: 单位为厘米
:param target_height: 单位为厘米
"""
# 将厘米转成像素
target_width = cm_to_px(target_width)
target_height = cm_to_px(target_height)doc = fitz.open(input_file) page0 = doc[0] width = page0.mediabox.width height = page0.mediabox.height print(f'width:{width} height:{height}') new_doc = fitz.open() # 创建新文档 for page in doc: # 创建新页面(指定目标尺寸) new_page = new_doc.new_page(width=target_width, height=target_height) # 计算源页面到目标页面的转换矩阵 matrix = fitz.Matrix(target_width / page.rect.width, target_height / page.rect.height) print('matrix0:', matrix) width = page.mediabox.width height = page.mediabox.height print(f'width:{width} height:{height}') print(f'width:{width * 25.4 / 72} height:{height * 25.4 / 72}') print(page.mediabox) new_page.show_pdf_page( new_page.rect, # 目标区域(使用整个新页面) doc, # 源文档 page.number # 页码 ) new_doc.save(output_file) doc.close() new_doc.close() print(doc.is_closed) try: os.chmod(input_file, 0o777) os.remove(input_file) os.rename(output_file, input_file) return input_file except Exception as e: print(e) return output_fileif name == "main":
# 修改pdf页面尺寸大小,例如将pdf页面尺寸修改成15cm*20cm
input_pdf = r"C:\Users\EDY\Desktop\xxxxx\xxxx.pdf"
output_pdf = input_pdf.replace('.pdf', '_1.pdf')
resize_pdf_pages(input_pdf, output_pdf, 15, 20) -
将两个pdf拼接在一起
def merge_pages_vertically(page1, page2):
"""将两个页面垂直合并(上下排列)
Args:
page1: 源页面1 (fitz.Page对象)
page2: 源页面2 (fitz.Page对象)
Returns:
fitz.Document: 包含合并后页面的新文档
"""
# 1. 创建新文档
new_doc = fitz.open()# 2. 计算新页面尺寸 w1, h1 = page1.rect.width, page1.rect.height w2, h2 = page2.rect.width, page2.rect.height new_width = max(w1, w2) new_height = h1 + h2 # 3. 创建新页面 new_page = new_doc.new_page(width=new_width, height=new_height) # 4. 定义目标区域 rect_top = fitz.Rect(0, 0, new_width, h1) # 顶部区域 rect_bottom = fitz.Rect(0, h1, new_width, new_height) # 底部区域 # 5. 将源页面绘制到新页面(保留原始尺寸) new_page.show_pdf_page(rect_top, page1.parent, page1.number) new_page.show_pdf_page(rect_bottom, page2.parent, page2.number) return new_docdef merge_pdf(pdf1, pdf2, save_pdf):
# 合并pdf
doc1 = fitz.open(pdf1)
doc2 = fitz.open(pdf2)
# 获取要合并的页面(此处选择第一页)
page1 = doc1[0]
page2 = doc2[0]
# 垂直合并
merged_doc = merge_pages_vertically(page1, page2)
# 保存结果
merged_doc.save(save_pdf)
# 关闭文档
doc1.close()
doc2.close()
merged_doc.close()if name == "main":
input_pdf = r"C:\Users\EDY\Desktop\xxxxx\xxxx.pdf"
# 上下合并两个pdf
save_pdf = input_pdf.replace('.pdf', '_2.pdf')
merge_pdf(input_pdf, input_pdf, save_pdf)
像WPS Office 一样处理pdf页面尺寸
FOAF-lambda2025-08-28 7:04
相关推荐
weixin_4410036410 小时前
2027徐涛《核心考案+优题库》电子版pdfIT大师兄吖10 小时前
paddleOcr 懒人整合包 添加pdf和图片转markdown 添加GPU支持IT大师兄吖11 小时前
PaddleOCR-VL-1.5 懒人整合包 支持PDF转MD 比PP-StructureV3更精准AmyLin_200112 小时前
【pdf2md-3:实现揭秘】福昕PDF SDK Python 开发实战:从逐字符提取到 LR 版面分析开开心心就好13 小时前
伪装文件历史记录!修改时间的黑科技软件其实秋天的枫14 小时前
【26最新】考研计算机408统考历年真题及答案解析PDF电子版(2009-2026年)优化控制仿真模型14 小时前
【26最新】考研计算机408统考历年真题及答案解析PDF电子版(2009-2026年)IT大师兄吖15 小时前
小红书FireRed-OCR 2B 图片和PDF转md 懒人整合包SEO-狼术16 小时前
Secure PDF Delphi Editionlong_songs16 小时前
纯前端 PNG/JPG 转 PDF 工具(无需服务器,源码分享)