-
修改页面尺寸
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 天前
Office转PDF转换器v1.0.pyreembarkation3 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现reembarkation3 天前
vue-pdf 实现blob数据的预览Light603 天前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度(一气呵成)伟贤AI之路3 天前
【分享】中小学教材课本 PDF 资源获取指南东风西巷3 天前
PDFgear:免费全能的PDF处理工具Sunny_yiyi4 天前
Java根据模版导出PDF文件小*-^-*九4 天前
php 使用html 生成pdf word wkhtmltopdf 系列2千册4 天前
pyside6 的pdf显示测试 -- 01qq_172805595 天前
Go 语言 PDF 生成库综合比较与实践指南