search_keyword.py 882 B

1234567891011121314151617181920212223
  1. search_word = '大谷翔平'
  2. from docx import Document
  3. from docx.shared import RGBColor
  4. doc = Document('/Users/mac/Documents/ottani.docx')
  5. search_word = '大谷翔平'
  6. for paragraph in doc.paragraphs:
  7. if search_word in paragraph.text:
  8. for run in paragraph.runs:
  9. print('1',run.text)
  10. if search_word in run.text:
  11. new_run = paragraph.add_run(run.text)
  12. for i, char in enumerate(run.text):
  13. if char == search_word[0] and run.text[i:i + len(search_word)] == search_word:
  14. new_run.font.color.rgb = RGBColor(255, 255, 0) # Yellow color
  15. new_run.text = new_run.text[:i] + search_word + new_run.text[i + len(search_word):]
  16. paragraph.runs.remove(run)
  17. break
  18. doc.save('/Users/mac/Documents/ottani_mark.docx')