"""
本项目特点:
1、获取word中数据,批量替换word中文字!
"""
import os
from docx import Document
def replace_word(doc, old_word, new_word):
"""
定义批量替换文字的函数
:param doc: 要替换的文档
:param old: 被替换的文字
:param new: 替换后的文字
:return:
"""
#ss = False
for p in doc.paragraphs: # 遍历文档段落
for run in p.runs: # 遍历段落的字块
#if "一般公共预算当年拨款结构情况" in run.text:
#ss = True
#elif "一般公共预算当年拨款具体使用情况" in run.text:
#ss = False
#if ss:
#if "(类)" not in run.text:
if old_word in run.text:
run.text = run.text.replace(old_word, new_word)
# 遍历文档的表格, 替换表格里的要替换的文字
# for table in doc.tables:
# for row in table.rows:
# for cell in row.cells:
# cell.text = cell.text.replace(old_word, new_word)
data_text = [
{"old": "一般公共服务支出", "new": "一般公共服务(类)支出"},
{"old": "外交支出", "new": "外交(类)支出"},
{"old": "国防支出", "new": "国防(类)支出"},
{"old": "公共安全支出", "new": "公共安全(类)支出"},
{"old": "教育支出", "new": "教育(类)支出"},
{"old": "科学技术支出", "new": "科学技术(类)支出"},
{"old": "文化旅游体育与传媒支出", "new": "文化旅游体育与传媒(类)支出"},
{"old": "社会保障和就业支出", "new": "社会保障和就业(类)支出"},
{"old": "社会保险基金支出", "new": "社会保险基金(类)支出"},
{"old": "卫生健康支出", "new": "卫生健康(类)支出"},
{"old": "节能环保支出", "new": "节能环保(类)支出"},
{"old": "城乡社区支出", "new": "城乡社区(类)支出"},
{"old": "农林水支出", "new": "农林水(类)支出"},
{"old": "交通运输支出", "new": "交通运输(类)支出"},
{"old": "资源勘探信息等支出", "new": "资源勘探信息等(类)支出"},
{"old": "商业服务业等支出", "new": "商业服务业等(类)支出"},
{"old": "金融支出", "new": "金融(类)支出"},
{"old": "援助其他地区支出", "new": "援助其他地区(类)支出"},
{"old": "自然资源海洋气象等支出", "new": "自然资源海洋气象等(类)支出"},
{"old": "住房保障支出", "new": "住房保障(类)支出"},
{"old": "粮油物资储备支出", "new": "粮油物资储备(类)支出"},
{"old": "国有资本经营预算支出", "new": "国有资本经营预算(类)支出"},
{"old": "灾害防治及应急管理支出", "new": "灾害防治及应急管理(类)支出"},
{"old": "其他支出", "new": "其他(类)支出"},
{"old": "转移性支出", "new": "转移性(类)支出"},
{"old": "债务还本支出", "new": "债务还本(类)支出"},
{"old": "债务付息支出", "new": "债务付息(类)支出"},
{"old": "债务发行费用支出", "new": "债务发行费用(类)支出"},
{"old": "抗疫特别国债还本支出", "new": "抗疫特别国债还本(类)支出"},
]
for i in range(100):
try:
path = input(str(i + 1) + ':请输入文件夹地址:').replace('\\', '/')
for file_name in os.listdir(path):
print(file_name)
doc_path = path + '/' + file_name
doc = Document(doc_path)
for data in data_text:
replace_word(doc, data["old"], data["new"])
doc.save(file_name)
print("================ 完成个文件 =================")
input('=========================================')
except Exception as E:
print('错误:', '1.文件夹里的文件不是”.docx“类型文件! 2.文件地址有无(”D:\”)')
https://gitee.com/hayratjan点击打开海哥Gitee