Python Forum

Full Version: python-docx- change lowercase to bold, italic
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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