1

言い換えれば、引用符で囲まれていない段落内の単語のみを置き換えたいと思います。

import sys, os.path, win32com.client 
app = win32com.client.Dispatch('Word.Application') 

doc = app.Documents.Open(input_fil1) 
incontent = doc.Content.Text app.Quit() 

# want to replace what's is not inside the quote. 
otcontent = incontent.replace('No', 'Yes') 

print "New content:" print otcontent 
4

1 に答える 1

0

引用符の区切り記号に従ってテキストをスライスし、位置がペアになっている部分文字列のみを処理します。

string = '''Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. If text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation. Therefore publishers use lorem ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, the use of lorem ipsum is often referred to as greeking, from the phrase "it's all Greek to me", which indicates that something is not meant to be readable text (although greeking can also mean somewhat different things; see the article for details)'''

l = string.split('"')
new_l = []
for position,substring in list(enumerate(l)):
    if position%2 == 0:
        s = substring.replace('lorem','STACK OVERFLOW')
        new_l.append(s)
    else:
        new_l.append(substring)


new_string = '"'.join(new_l)

print new_string

出力:

Even though "lorem ipsum" may arouse curiosity because of its resemblance to classical Latin, it is not intended to have meaning. If text is comprehensible in a document, people tend to focus on the textual content rather than upon overall presentation. Therefore publishers use STACK OVERFLOW ipsum when displaying a typeface or design elements and page layout in order to direct the focus to the publication style and not the meaning of the text. In spite of its basis in Latin, the use of STACK OVERFLOW ipsum is often referred to as greeking, from the phrase "it's all Greek to me", which indicates that something is not meant to be readable text (although greeking can also mean somewhat different things; see the article for details)

最初の「lorem」は引用符に含まれているため無視されます。

于 2012-11-29T21:47:25.980 に答える