프로그래밍/python
python-docx 문자열에서 특정 문자만 색상을 적용하는 코드
이휘재123
2023. 4. 3. 17:58
반응형
from docx import Document
from docx.shared import RGBColor
document = Document()
# 문자열 생성
text = "이 문장에서는 특정 문자열만 빨간색으로 표시합니다."
# 문서에 텍스트 추가
paragraph = document.add_paragraph()
# 텍스트를 런 객체로 분리
runs = []
for word in text.split():
run = paragraph.add_run(word + " ")
if "특정" in word:
font = run.font
font.color.rgb = RGBColor(255, 0, 0) # 빨간색으로 설정
runs.append(run)
# 워드 파일 저장
document.save('example.docx')
반응형