Python Word實(shí)現(xiàn)批量替換文本并生成副本
任務(wù)背景
為提高辦公效率,用python試手了一個(gè)word任務(wù),要求如下:
給你一個(gè)基礎(chǔ)word文檔A,格式為docx,名字為:A.docx。A文檔中有表格和文字,要求是將里面的字符串"完成繪畫"分別替換成完成制作款式x和復(fù)習(xí)制作款式x,輸出相應(yīng)副本,命名為對(duì)應(yīng)序號(hào)增序文檔,如:1、A.docx, 2、A.docx。
要求是輸出1000份這樣的增序文檔。
編碼思路
從問題中可提煉以下實(shí)現(xiàn)思路:
初始化,輸入目標(biāo)目錄、文件命名格式、待替換源字符串、目標(biāo)字符串
支持文檔段落和表格內(nèi)容查找,支持文本替換
文本增序和命名增序處理
效果預(yù)覽:


代碼實(shí)現(xiàn)
文件名:doc_copy_replace.py
代碼如下:
# -*- 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:
#遍歷表格段落內(nèi)容,回到上個(gè)步驟,將cell當(dāng)作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 = '復(fù)習(xí)制作款式'
# # 單樣例測(cè)試
# 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 = '復(fù)習(xí)制作款式'
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)到此這篇關(guān)于Python Word實(shí)現(xiàn)批量替換文本并生成副本的文章就介紹到這了,更多相關(guān)Python Word批量替換文本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python保存網(wǎng)頁上的圖片或者保存頁面為截圖
這篇文章主要介紹了使用Python保存網(wǎng)頁上的圖片或者保存頁面為截圖的方法,保存網(wǎng)頁圖片主要用到urllib模塊,即簡(jiǎn)單的爬蟲原理,需要的朋友可以參考下2016-03-03
10個(gè)使用Python必須知道的內(nèi)置函數(shù)
這篇文章小編主要向大家介紹的是10個(gè)使用Python必須知道的內(nèi)置函數(shù)reduce()、split()、map()等,更多后置函數(shù)請(qǐng)看下文2021-09-09
python檢查序列seq是否含有aset中項(xiàng)的方法
這篇文章主要介紹了python檢查序列seq是否含有aset中項(xiàng)的方法,涉及Python針對(duì)序列的相關(guān)判斷技巧,需要的朋友可以參考下2015-06-06
10個(gè)Python辦公自動(dòng)化案例總結(jié)
Python作為一種簡(jiǎn)單而強(qiáng)大的編程語言,不僅在數(shù)據(jù)科學(xué)和軟件開發(fā)領(lǐng)域廣受歡迎,還在辦公自動(dòng)化方面發(fā)揮了巨大作用,通過Python,我們可以編寫腳本來自動(dòng)執(zhí)行各種重復(fù)性任務(wù),從而提高工作效率并減少錯(cuò)誤,在本文中,我們總結(jié)了10個(gè)Python辦公自動(dòng)化案例2024-09-09
深入解析Python設(shè)計(jì)模式編程中建造者模式的使用
這篇文章主要介紹了深入解析Python設(shè)計(jì)模式編程中建造者模式的使用,建造者模式的程序通常將所有細(xì)節(jié)都交由子類實(shí)現(xiàn),需要的朋友可以參考下2016-03-03

