Python 批量修改 Word 文档中图片的大小并居中对齐

Python 批量修改 Word 文档中图片的大小并居中对齐

错过,再遇见可能就难了!此时,你是你,我是我,再遇见,可就真的你是你,我是我,没有一丝的牵连纠缠------

目标

  1. 批量修改 Word 文档中图片的大小。
  2. 将图片在文档中居中对齐。

所需工具

  • Python 3
  • python-docx 库:用于处理 Word 文档。

安装依赖

首先,确保你已经安装了 python-docx 库。你可以通过以下命令安装它:

bash 复制代码
pip install python-docx

步骤

1. 准备工作

创建一个 Python 文件,例如 resize_and_center_images.py,并将以下代码粘贴到文件中。

2. 编写代码
python 复制代码
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH

# 定义函数来调整图片大小并居中对齐
def resize_and_center_images(doc_path, output_path, target_width_cm, target_height_cm):
    # 打开Word文档
    doc = Document(doc_path)

    # 转换厘米为EMU(English Metric Units),1厘米 = 360000 EMU
    target_width_emu = int(target_width_cm * 360000)
    target_height_emu = int(target_height_cm * 360000)

    # 遍历文档中的所有行内形状(图片)
    for shape in doc.inline_shapes:
        # 修改图片大小
        shape.width = target_width_emu
        shape.height = target_height_emu
        
        # 获取图片所在的段落
        paragraph = shape._inline.getparent().getparent().getparent()
        
        # 将图片所在段落设置为居中
        paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

    # 保存修改后的Word文档
    doc.save(output_path)

# 示例用法
if __name__ == "__main__":
    # 输入文档路径
    input_doc = './管理后台测试问题2轮.docx'
    # 输出文档路径
    output_doc = './管理后台测试问题2轮2.docx'
    # 目标图片大小(厘米)
    width_cm = 14.64
    height_cm = 9.27

    # 调用函数
    resize_and_center_images(input_doc, output_doc, width_cm, height_cm)

代码解释

  1. 导入必要的模块

    python 复制代码
    from docx import Document
    from docx.enum.text import WD_ALIGN_PARAGRAPH
  2. 定义 resize_and_center_images 函数

    • 参数:

      • doc_path: 输入的 Word 文档路径。
      • output_path: 输出的 Word 文档路径。
      • target_width_cm: 目标图片宽度(厘米)。
      • target_height_cm: 目标图片高度(厘米)。
    • 打开文档:

      python 复制代码
      doc = Document(doc_path)
    • 转换单位 :

      将厘米转换为 EMU(1厘米 = 360000 EMU)。

      python 复制代码
      target_width_emu = int(target_width_cm * 360000)
      target_height_emu = int(target_height_cm * 360000)
    • 遍历并调整图片大小:

      python 复制代码
      for shape in doc.inline_shapes:
          shape.width = target_width_emu
          shape.height = target_height_emu
    • 居中对齐:

      python 复制代码
      paragraph = shape._inline.getparent().getparent().getparent()
      paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
    • 保存文档:

      python 复制代码
      doc.save(output_path)
  3. 运行示例

    • 运行代码文件时,将输入和输出路径、目标图片大小传递给 resize_and_center_images 函数。

注意事项

  • 确保路径和文件名正确,并且文档中确实包含图片。
  • 图片的居中对齐是基于图片所在的段落进行的,确保图片位于段落中。

结论

这个教程展示了如何使用 python-docx 库批量修改 Word 文档中的图片大小,并将其居中对齐。通过调整代码中的参数,你可以自定义图片的尺寸和输出路径。

如果有任何问题或需要进一步的帮助,请随时联系我!

相关推荐
紫雾凌寒34 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
HH牛码2 小时前
C# 中 Array、ArrayList 和 List 的比较
开发语言·c#
HH牛码2 小时前
C#学生管理系统 进阶(通过接口,继承接口的类,实现接口约束_对List中存储的数据进行排列)
c#
吾与谁归in3 小时前
C#实现本地Deepseek模型及其他模型的对话
人工智能·c#·wpf·deepseek
快乐点吧3 小时前
【word】保存重开题注/交叉引用消失,更新域问题
word
子蛟7 小时前
Get a free SSL certificate interface.
c#·ssl
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器