python操作word文档替换文字快捷键_如何使用python-docx搜索和替换word文档中的单词/文本...

importdocxdefdocx_find_replace_text(doc,search_text,replace_text):paragraphs=list(doc.paragraphs)fortindoc.tables:forrowint.rows:forcellinrow.cells:forparagraphincell.paragraphs:paragraphs.append(paragraph)forpinparagraphs:ifsearch_textinp.text:inline=p.runs# Replace strings and retain the same style.# The text to be replaced can be split over several runs so# search through, identify which runs need to have text replaced# then replace the text in those identifiedstarted=Falsesearch_index=0# found_runs is a list of (inline index, index of match, length of match)found_runs=list()found_all=Falsereplace_done=Falseforiinrange(len(inline)):# case 1: found in single run so short circuit the replaceifsearch_textininline[i].textandnotstarted:found_runs.append((i,inline[i].text.find(search_text),len(search_text)))text=inline[i].text.replace(search_text,str(replace_text))inline[i].text=text

replace_done=Truefound_all=Truebreakifsearch_text[search_index]notininline[i].textandnotstarted:# keep looking ...continue# case 2: search for partial text, find first runifsearch_text[search_index]ininline[i].textandinline[i].text[-1]insearch_textandnotstarted:# check sequencestart_index=inline[i].text.find(search_text[search_index])check_length=len(inline[i].text)fortext_indexinrange(start_index,check_length):ifinline[i].text[text_index]!=search_text[search_index]:# no match so must be false positivebreakifsearch_index==0:started=Truechars_found=check_length-start_index

search_index+=chars_found

found_runs.append((i,start_index,chars_found))ifsearch_index!=len(search_text):continueelse:# found all chars in search_textfound_all=Truebreak# case 2: search for partial text, find subsequent runifsearch_text[search_index]ininline[i].textandstartedandnotfound_all:# check sequencechars_found=0check_length=len(inline[i].text)fortext_indexinrange(0,check_length):ifinline[i].text[text_index]==search_text[search_index]:search_index+=1chars_found+=1else:break# no match so must be endfound_runs.append((i,0,chars_found))ifsearch_index==len(search_text):found_all=Truebreakiffound_allandnotreplace_done:fori,iteminenumerate(found_runs):index,start,length=[tfortinitem]ifi==0:text=inline[index].text.replace(inline[index].text[start:start+length],str(replace_text))inline[index].text=textelse:text=inline[index].text.replace(inline[index].text[start:start+length],'')inline[index].text=text# print(p.text)# sample usage as per exampledoc=docx.Document('find_replace_test_document.docx')docx_find_replace_text(doc,'Testing1','Test ')docx_find_replace_text(doc,'Testing2','Test ')docx_find_replace_text(doc,'rest','TEST')doc.save('find_replace_test_result.docx')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值