python怎么替换这个word里边内容

链接: https://pan.baidu.com/s/1cQvoiB76SG2EsdO612Xo6A?pwd=1234 提取码: 1234 复制这段内容下载台卡文件

不能替换这些艺术字!


import win32com

from win32com.client import Dispatch, constants

#模板文件保存路径,此处使用的是绝对路径,相对路径未测试过
filedir='c:\台卡1.docx'
template_path =docx.Document(r'c:\台卡1.docx')# 'C:\Users\yely\Desktop\jiangzhuang\print.doc'

#另存文件路径,需要提前建好文件夹,不然会出错
print(template_path)
store_path_dir='c:\123.docx'
store_path = target_Doc=docx.Document(r'c:\123.docx')#'C:\Users\yely\Desktop\jiangzhuang\list\\'

#模板中需要被替换的文本。u''中的u表示unicode字符,用于中文支持



#启动word

w = win32com.client.Dispatch('Word.Application')

# 或者使用下面的方法,使用启动独立的进程:

# w = win32com.client.DispatchEx('Word.Application')

# 后台运行,不显示,不警告

w.Visible = 0

w.DisplayAlerts = 0

# 打开新的文件

doc = w.Documents.Open(FileName='c:\台卡1.docx')

# worddoc = w.Documents.Add() # 创建新的文档

# 正文文字替换

w.Selection.Find.ClearFormatting()

w.Selection.Find.Replacement.ClearFormatting()

#名单

#lst = [u'张三', u'李四', u'王五']

#迭代替换名字,并以名字为名另存文件

#for i in lst:

 #   OldStr, NewStr = NewStr, i
OldStr = '3个字'
NewStr = 'AAA'

w.Selection.Find.Execute(OldStr, False, False, False, False, False, True, 1, True, NewStr,2)
#w.Selection.Find.Execute(FindText='3个字',ReplaceWith='bbb',Replace=2)
doc.SaveAs(store_path_dir  +'.doc')
print(store_path_dir + '.doc')
#doc.PrintOut()直接打印,未测试

doc.Close()

w.Documents.Close()

w.Quit()

gpt辅助 如果您想要用普通的字母替换这些艺术字,可以使用Python的string模块的maketrans和translate方法来实现。下面是一个例子:

import string

# 艺术字串
art_text = "𝓗𝓮𝓵𝓵𝓸, 𝔀𝓸𝓻𝓵𝓭!"

# 生成普通字母串和艺术字串对应的转换表
normal_text = string.ascii_letters + string.punctuation + string.digits + ' '
trans_table = str.maketrans(art_text, normal_text)

# 使用转换表进行替换
plain_text = art_text.translate(trans_table)

print(plain_text)


这个例子中,我们使用了Python的string模块,其中ascii_letters代表所有字母,punctuation代表所有标点符号,digits代表所有数字,最后加上一个空格组成了普通字母串normal_text。我们使用str.maketrans()方法生成了普通字母串和艺术字串对应的转换表trans_table,然后使用translate()方法将艺术字串art_text转换为普通字母串plain_text。

你想要怎么替换这些字,不是很明白你的需求

  • 这篇博客: Python查找两个word中的相同内容中的 三、源码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • # coding=utf-8
    
    from docx import Document
    import re, sys, datetime
    
    
    def getText(wordname):
        d = Document(wordname)
        texts = []
        for para in d.paragraphs:
            texts.append(para.text)
        return texts
    
    def is_Chinese(word):
        for ch in word:
            if '\u4e00' <= ch <= '\u9fff':
                return True
        return False
    
    def msplit(s, seperators = ',|\.|\?|,|。|?|!'):
        return re.split(seperators, s)
    
    def readDocx(docfile):
        print('*' * 80)
        print('文件', docfile, '加载中……')
        t1 = datetime.datetime.now()
        paras = getText(docfile)
        segs = []
        for p in paras:
            temp = []
            for s in msplit(p):
                if len(s) > 2:
                    temp.append(s.replace(' ', ""))
            if len(temp) > 0:
                segs.append(temp)
        t2 = datetime.datetime.now()
        print('加载完成,用时: ', t2 - t1)
        showInfo(segs, docfile)
        return segs
        
    def showInfo(doc, filename = 'filename'):
        chars = 0
        segs = 0
        for p in doc:
            for s in p:
                segs = segs + 1
                chars = chars + len(s)
        print('段落数: {0:>8d} 个。'.format(len(doc)))
        print('短句数: {0:>8d} 句。'.format(segs))
        print('字符数: {0:>8d} 个。'.format(chars))
              
    def compareParagraph(doc1, i, doc2, j, min_segment = 5): 
        """
        功能为比较两个段落的相似度,返回结果为两个段落中相同字符的长度与较短段落长度的比值。
        :param p1: 行
        :param p2: 列
        :param min_segment = 5: 最小段的长度
        """
        p1 = doc1[i]
        p2 = doc2[j]
        len1 = sum([len(s) for s in p1])
        len2 = sum([len(s) for s in p2])
        #print(len1)
        #print(len2)
        if len1 < 10 or len2 < 10:
            return []
        
        list = []
        for s1 in p1:
            if len(s1) < min_segment:
                continue;
            for s2 in p2:
                if len(s2) < min_segment:
                    continue;
                if s2 in s1:
                    list.append(s2)
                elif s1 in s2:
                    list.append(s1)
                    
        # 取两个字符串的最短的一个进行比值计算
        count = sum([len(s) for s in list])
        ratio = float(count) /  min(len1, len2)
        if count > 10 and ratio > 0.1:
            print(' 发现相同内容 '.center(80, '*'))
            print('文件1第{0:0>4d}段内容:{1}'.format(i + 1, p1))
            print('文件2第{0:0>4d}段内容:{1}'.format(j + 1, p2))
            print('相同内容:', list)
            print('相同字符比:{1:.2f}%\n相同字符数: {0}\n'.format(count, ratio * 100))
        return list
     
    #if len(sys.argv) < 3:
    #    print("参数小于2.")
    
    
    #doc1 = readDocx(sys.argv[1])
    #print(doc1)
    #doc2 = readDocx(sys.argv[2])
    #print(doc2)
    
    doc1 = readDocx('./document/doc1.docx')
    doc2 = readDocx('./document/doc2.docx')
    
    print('开始比对...'.center(80, '*'))
    t1 = datetime.datetime.now()
    for i in range(len(doc1)):
        if i % 100 == 0:
            print('处理进行中,已处理段落 {0:>4d} (总数 {1:0>4d} ) '.format(i, len(doc1)))
        for j in range(len(doc2)):
            compareParagraph(doc1, i, doc2, j)
     
    t2 = datetime.datetime.now() 
    print('\n比对完成,总用时: ', t2 - t1)   
你可以使用Python的python-docx库来操作docx文件,实现替换文本的功能。下面是一个示例代码:
import docx

# 打开原始文档和目标文档
source_doc = docx.Document('c:\\台卡1.docx')
target_doc = docx.Document()

# 替换文本
for paragraph in source_doc.paragraphs:
    new_text = paragraph.text.replace('3个字', 'AAA')
    target_doc.add_paragraph(new_text)

# 保存目标文档
target_doc.save('c:\\123.docx')
这里使用docx.Document函数来打开文档,paragraphs属性获取所有段落,text属性获取段落的文本内容。add_paragraph方法用于添加新的段落,save方法保存文档。

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
这里给出代码,可以实现替换除艺术字以外的文本内容,并保存为新的文件。

import docx

# 模板文件保存路径
filedir = 'c:/台卡1.docx'
template_path = docx.Document(filedir)

#另存文件路径
store_path_dir = 'c:/123.docx'
store_path = docx.Document()
# 遍历模板中的段落
for para in template_path.paragraphs:
    # 检查段落中的每一个run
    for run in para.runs:
        if run.font.name.lower() != 'arto':
            # 只有非艺术字的文本才进行替换
            text = run.text.replace('3个字', 'AAA')
            run.text = text
    # 将处理后的段落加入到新的文档中
    store_path.add_paragraph(para.text)

#保存为新文件
store_path.save(store_path_dir)
print('替换完成,已保存至{}'.format(store_path_dir))

需要注意的是,此代码使用了python-docx库来处理Word文档,需要提前使用pip安装:

pip install python-docx

如果我的回答解决了您的问题,请采纳!

该回答引用ChatGPT
这段代码中的文本替换部分使用了win32com库来操作Word文档,具体实现是通过调用Word的Selection.Find.Execute方法来实现的。下面是代码中的替换部分:

OldStr = '3个字'
NewStr = 'AAA'
w.Selection.Find.Execute(OldStr, False, False, False, False, False, True, 1, True, NewStr, 2)

其中,OldStr是需要被替换的文本,NewStr是替换后的文本。这里使用了Selection.Find.Execute方法来查找并替换文本,具体参数含义如下:
- FindText:需要查找的文本;
- MatchCase:是否区分大小写;
- MatchWholeWord:是否匹配整个单词;
- MatchWildcards:是否使用通配符;
- MatchSoundsLike:是否匹配发音相似的单词;
- MatchAllWordForms:是否匹配所有单词形式;
- Forward:查找方向,True表示向前查找,False表示向后查找;
- Wrap:是否循环查找;
- Format:查找格式,0表示不查找格式,1表示查找格式,2表示查找并替换格式;
- ReplaceWith:替换后的文本;
- Replace:替换方式,0表示不替换,1表示替换第一个匹配项,2表示替换所有匹配项。
如果需要替换多个文本,可以使用循环来实现,例如:

replace_dict = {'3个字': 'AAA', '4个字': 'BBB', '5个字': 'CCC'}
for old_str, new_str in replace_dict.items():
w.Selection.Find.Execute(old_str, False, False, False, False, False, True, 1, True, new_str, 2)

这里使用了一个字典来存储需要替换的文本和替换后的文本,然后通过循环来逐个替换。

http://t.csdn.http//t.csdn.cn/Zq2hJ

这段代码中的文本替换部分是使用win32com库来操作Word进行的,具体实现是通过`w.Selection.Find.Execute()`方法来查找并替换文本。其中,`OldStr`是需要被替换的文本,`NewStr`是替换后的文本。
如果要替换多个文本,可以使用一个列表来存储需要替换的文本,然后使用`for`循环来遍历列表,每次替换一个文本。
例如,如果要将文本中的"3个字"替换为"AAA",可以将以下代码:

OldStr = '3个字'
NewStr = 'AAA'
w.Selection.Find.Execute(OldStr, False, False, False, False, False, True, 1, True, NewStr, 2)

修改为:

replace_list = [('3个字', 'AAA'), ('4个字', 'BBB'), ('5个字', 'CCC')]
for old_str, new_str in replace_list:
w.Selection.Find.Execute(old_str, False, False, False, False, False, True, 1, True, new_str, 2)