와 Word 문서에서 텍스트의 스타일을 변경할 수 없습니다 나는 텍스트내가 파이썬 DOCX
안녕하세요을 포함하는 워드 문서를 만들었습니다. 빚진 $ {debt}. 곧 다시 돌려주세요.
(Times New Roman 크기 12). 파일 이름은 debtTemplate.docx입니다. {debt}을 python-docx를 사용하여 실제 숫자 (1.20)로 바꾸고 싶습니다. 나는 시도 그 코드 다음
from docx import Document
document = Document("debtTemplate.docx")
paragraphs = document.paragraphs
debt = "1.20"
paragraph = paragraphs[0]
text = paragraph.text
newText = text.format(debt=debt)
paragraph.clear()
paragraph.add_run(newText)
document.save("debt.docx")
이 원하는 텍스트를 새 문서를 초래하지만, Calabri 글꼴 크기 11에 내가 원래처럼되고 글꼴을 싶습니다 타임즈 새로운 로마 크기 12
paragraph.add_run()
에 스타일 변수를 추가 할 수 있다는 것을 알고 있으므로 아무 것도 시도하지 않았습니다. 예 : paragraph.add_run(newText,style="Strong")
도 아무 것도 변경하지 않았습니다.
내가 할 수있는 것을 아는 사람이 있습니까?
편집 : 여기에 수정 된 버전의 코드가 있지만 작동하지 않았 으면합니다.
from docx import Document
document = Document("debtTemplate.docx")
document.save("debt.docx")
paragraphs = document.paragraphs
debt = "1.20"
paragraph = paragraphs[0]
style = paragraph.style
text = paragraph.text
newText = text.format(debt=debt)
paragraph.clear()
paragraph.add_run(newText,style)
document.save("debt.docx")
조금 늦게 ... Times New Roman과 12 크기를 모두 만들고 싶습니까? –