Python世界:自动化办公Word之批量替换文本生成副本

Python世界:自动化办公Word之批量替换文本生成副本

任务背景

为提高办公效率,用python试手了一个word任务,要求如下:

给你一个基础word文档A,格式为docx,名字为:A.docx。A文档中有表格和文字,要求是将里面的字符串"完成绘画"分别替换成完成制作款式x复习制作款式x,输出相应副本,命名为对应序号增序文档,如:1、A.docx, 2、A.docx

要求是输出1000份这样的增序文档。

编码思路

从问题中可提炼以下实现思路:

  • 初始化,输入目标目录、文件命名格式、待替换源字符串、目标字符串
  • 支持文档段落和表格内容查找,支持文本替换
  • 文本增序和命名增序处理

效果预览:

代码实现

文件名:doc_copy_replace.py

代码如下:

python 复制代码
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 22:20:16 2024
@author: 来知晓
"""

from docx import Document


def read_ducment(old, new, document):
    # 遍历文档
    for paragraph in document.paragraphs:
        for run in paragraph.runs:
            #替换功能
            if old in run.text:
                run.text=run.text.replace(old,new)
 
    # 遍历表格
    for table in document.tables:
        for row in table.rows:
            for cell in row.cells:
                #遍历表格段落内容,回到上个步骤,将cell当作paragraph处理
                for paragraph in cell.paragraphs:
                    for run in paragraph.runs:
                        #替换功能
                        if old in cell.text:
                            run.text=run.text.replace(old,new)


# doc_path = r'D:\iocode\来知晓\tmp\A.docx'
# doc_new_path = r'D:\iocode\来知晓\tmp\new.docx'
# str_src = '完成绘画'
# str_tar_odd = '完成制作款式'
# str_tar_even = '复习制作款式'

# # 单样例测试
# document = Document(doc_path)
# read_ducment(str_src, str_tar, document)
# document.save(doc_new_path)


# 正式demo
cnt = 1000
doc_new_dir = r'D:\iocode\来知晓\tmp'
doc_path_origin = r'D:\iocode\来知晓\tmp\A.docx'
str_src = '完成绘画'
str_tar_odd = '完成制作款式'
str_tar_even = '复习制作款式'

cnt_d2 = cnt // 2
str_split = '\\'

for i in range(cnt_d2):
    k = i + 1
    str_file_name = r'、A.docx'
    doc_new_path_odd = doc_new_dir  + str_split + str(2*k-1) + str_file_name
    str_tar_odd_conca = str_tar_odd + str(k)
    document_odd = Document(doc_path_origin)
    read_ducment(str_src, str_tar_odd_conca, document_odd)
    document_odd.save(doc_new_path_odd)
    
    doc_new_path_even = doc_new_dir + str_split + str(2*k) + str_file_name
    str_tar_even_conca = str_tar_even + str(k)
    document_even = Document(doc_path_origin)
    read_ducment(str_src, str_tar_even_conca, document_even)
    document_even.save(doc_new_path_even)

相关参考

  1. python-docx替换文档段落及表格table指定内容,不改变格式方法
  2. Python 如何使用python-docx替换Word文档中的文本并保存
相关推荐
CodeCraft Studio1 小时前
CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
python·photoshop·cad·aspose·aspose.cad
Python×CATIA工业智造3 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
onceco3 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
狐凄4 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
笑稀了的野生俊7 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva7 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm
路来了7 小时前
Python小工具之PDF合并
开发语言·windows·python
蓝婷儿7 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
AntBlack8 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python