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 文档中的图片大小,并将其居中对齐。通过调整代码中的参数,你可以自定义图片的尺寸和输出路径。

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

相关推荐
故事不长丨7 分钟前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对
飞鸟真人9 分钟前
关于python -m http.server的一些安全问题
python·安全·http
tjjucheng15 分钟前
小程序定制开发哪家性价比高
python
No0d1es26 分钟前
2025年12月 GESP CCF编程能力等级认证Python六级真题
python·青少年编程·gesp·ccf·六级
亮子AI1 小时前
【Python】比较两个cli库:Click vs Typer
开发语言·python
CappuccinoRose1 小时前
流计算概述
python·flink·流计算·数据流·pyflink
Dragon水魅1 小时前
Fandom Wiki 网站爬取文本信息踩坑实录
爬虫·python
Darkershadow1 小时前
蓝牙学习之unprovision beacon
python·学习·ble
小龙在山东1 小时前
基于 plumbum 跨平台执行Shell脚本
python
CCPC不拿奖不改名2 小时前
python基础:python语言中的函数与模块+面试习题
开发语言·python·面试·职场和发展·蓝桥杯