-
修改页面尺寸
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
相关推荐
技术程序猿华锋2 小时前
学术/报告场景实测:从申请OpenAI API Key获取并实现GPT-5 PDF分析机器人(含源码)花落文心2 小时前
使用 html2canvas + jspdf 实现页面元素下载为pdf文件阿幸软件杂货间2 小时前
PDF补丁丁:开源多年,完全免费的多功能 PDF 工具箱林枫依依18 小时前
PDF转图片、图片转PDF(免费)qq_5469372718 小时前
功能强大的PDF工具箱-- PDF补丁丁,v1.1.0.4657新版本,免费无广告,开箱即用版~界面开发小八哥18 小时前
.NET表格控件Spread .NET v18.0——支持富文本、增强PDF导出明天过后012219 小时前
PDF文件中的相邻页面合并成一页,例如将第1页和第2页合并,第3页和第4页合并爱上纯净的蓝天1 天前
迁移面试题UtopianCoding1 天前
MinerU:重新定义PDF智能提取的开源利器