1234567891011121314151617181920212223 |
- search_word = '大谷翔平'
- from docx import Document
- from docx.shared import RGBColor
- doc = Document('/Users/mac/Documents/ottani.docx')
- search_word = '大谷翔平'
- for paragraph in doc.paragraphs:
- if search_word in paragraph.text:
- for run in paragraph.runs:
- print('1',run.text)
- if search_word in run.text:
- new_run = paragraph.add_run(run.text)
- for i, char in enumerate(run.text):
- if char == search_word[0] and run.text[i:i + len(search_word)] == search_word:
- new_run.font.color.rgb = RGBColor(255, 255, 0) # Yellow color
- new_run.text = new_run.text[:i] + search_word + new_run.text[i + len(search_word):]
- paragraph.runs.remove(run)
- break
- doc.save('/Users/mac/Documents/ottani_mark.docx')
|