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

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

相关推荐
IvanCodes2 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
Patrick在香港2 小时前
Claude 工具调用返回空:8 次失败里只有 1 次状态码不对,其余全带 200
爬虫·python·api·claude·香港
Web3&Basketball3 小时前
CRM Agent 后训练实战:3 倍更少错误
python·架构·大模型·agent·推理
AIFQuant4 小时前
ETF行情API接入踩坑记:从报错到跑通的七个问题
python·金融·区块链·etf·基金
淡海水4 小时前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
GPU实战笔记5 小时前
云端 Python 开发:JupyterLab 还是 VS Code Remote-SSH?
python·vs code·jupyterlab·remote-ssh·远程开发
asdzx675 小时前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
狗狗狗狗狗乐啊6 小时前
搭一个 AI 对话工作台 AChat:从 0 到可用的完整记录(一)
python·react.js·ai编程
白猫不黑6 小时前
Python实现简易Web弱口令爆破与防护方案
python·web安全·计算机·网络安全·黑客·信息安全·渗透测试
kuuailetianzi7 小时前
Python初识:定位、优势与发展历程
python