像WPS Office 一样处理pdf页面尺寸

  1. 修改页面尺寸

    import os
    import shutil
    import fitz # PyMuPDF

    def 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)

  2. 将两个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)

相关推荐
奋斗的袍子00731 分钟前
Spring Boot 项目实现Word转PDF
spring boot·pdf·word
开开心心_Every20 小时前
强制打字练习工具:打够百字才可退出
java·游戏·微信·eclipse·pdf·excel·语音识别
开开心心_Every21 小时前
多端免费远程控制工具:4K流畅同账号直连
游戏·macos·微信·pdf·excel·语音识别·phpstorm
心语星光1 天前
用LibreOffice实现批量将pptx文件转换为pdf文件
开发语言·pdf·ppt
夏日白云1 天前
《PDF解析工程实录》第 17 章|内容流里“看得见却看不见”的字符:那些幽灵文字从哪来?
pdf·llm·大语言模型·rag·文档解析
2501_930707781 天前
使用C#代码检查 PDF 是否受密码保护并确认正确的密码
pdf
开开心心就好2 天前
图片格式转换工具,右键菜单一键转换简化
linux·运维·服务器·python·django·pdf·1024程序员节
貂蝉空大2 天前
vue-pdf-embed分页预览解决文字丢失问题
前端·vue.js·pdf
袁袁袁袁满2 天前
Python爬虫下载PDF文件
爬虫·python·pdf·python爬虫下载pdf文件
Knight_AL2 天前
docx4j vs LibreOffice:Java 中 Word 转 PDF 的性能实测
java·pdf·word