-
修改页面尺寸
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_px
def 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_file
if 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_doc
def 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
相关推荐
2501_929157681 天前
FC和SFC的原版说明书(扫描的PDF)zhangfeng11332 天前
R 导出 PDF 时中文不显示 不依赖 showtext** 的最简方案(用 extrafont 把系统 TTF 真正灌进 PDF 内核)pc大老2 天前
PDF文件翻译新方法:轻松多语言转换!SEO-狼术3 天前
Use Sticky Notes for Easier PDF Collaboration私人珍藏库5 天前
[Windows] PDF 专业电子签章工具 v3.3广都--编程每日问5 天前
deepseek 的对话json导出成word和pdflivingbody6 天前
【2025年9月版 亲测可用】《人民日报》PDF文件下载私人珍藏库6 天前
[Windows] 发票识别工具。支持xml、pdf、ofd文件超人在良家-阿启6 天前
PDF中表格的处理 (OCR)reasonsummer6 天前
【办公类-115-01】20250920职称资料上传01——多个jpg转同名PDF(如:荣誉证书)并自动生成单一文件夹