Python-docx 用连续编号替换字符串

Python-docx replacing string with consecutive numbering

我有一个文档,我将“#”作为占位符,我想用连续编号替换它

例如:

# Foo
# Bar 
# Baz
# Hello #

将替换为:

1 Foo
2 Bar 
3 Baz
4 Hello 5

这是我试过的代码,由于作用域问题,它会将变量全部替换为 0:

from docx import Document

document = Document('old.docx')

for p in document.paragraphs:
    inline = p.runs
    key = "#"
    count = 0
    for i in range(len(inline)):
        text = inline[i].text
        if key in text:

             text=text.replace(key,str(count),1)
             inline[i].text = text
             count += 1  #Not updating due to scoping issue


document.save('new.docx')

我很确定这不是范围问题,更像是一个小的循环错误。已修复并测试。

from docx import Document

document = Document('old.docx')
count = 1 #Initialize the count here.
for p in document.paragraphs: #apparently each line is a paragraph
    inline = p.runs
    key = "#"
    #count = 0 #noticed that this was resetting to 0....at each line
    for i in range(len(inline)):
        text = inline[i].text #This actually returns the entire line of characters I.e.(# Bar)
        for char in range(len(text)): #so now we need to go through the characters in the line
        
            if key == text[char]: #IF THE key is the character we replace the character with our count

                text=text.replace(key,str(count),1)
                count += 1  #It was resetting in your loop earlier
            inline[i].text = text #Now we reassign the entire char chain to inline.

document.save('new.docx')

让我们考虑一个字符串的解决方案。我会简单地使用正则表达式,因为在我看来它更通用。

from docx import Document
import re
from itertools import count

document = Document('Foo.docx')
occurences = 0
counter = count(occurences)
marker_string = "replace_me" #Just for show (You can do for ANY type of char/string or sets of string)
target_expression = re.compile("replace\_me") #note the escape for special characters

for p in document.paragraphs:
    inline = p.runs
    #count = 0
    for i in range(len(inline)):
        text = inline[i].text
        newtext = re.sub(target_expression,lambda x:  str(next(counter)),text) #we use regex sub function with lambda 
        #to sub in our iter tools counter.
        inline[i].text = newtext
               


document.save('new.docx')