Python Forum
python-docx- change lowercase to bold, italic - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python-docx- change lowercase to bold, italic (/thread-37616.html)



python-docx- change lowercase to bold, italic - Tmagpy - Jul-01-2022

In my docx file there are pieces of text i have to process such as making words italic, bold or underlined.

Example

Quote: input: In the future, if you try hard you will fail a lot


I used this code:

from docx import Document
import re

def change(filename,key):
    doc = Document(filename)
    for p in doc.paragraphs:
        text = p.text
        split_text = text.split(key)
        for i in split_text[:-1]:
            p.add_run(i)
            font = p.add_run(key).bold = True
        p.add_run(split_text[-1])
    doc.save('test.docx')
path = r'path of file'
change(path,'key')
but the result is
Quote:In the future, if you try hard you will fail a lotIn the future

What do I have to do so that the output is

Quote:output: In the future, if you try hard you will fail a lot