python-docx adding return key symbols in find and replace if specified new line

  ms-word, newline, paragraph, python-3.x, python-docx

My Code:-

from docx import Document



#open the document
doc=Document('teest.docx')


Dictionary = {
"number one ":"",
"number two ":"nn",
"number three ":"nn",
"number four ":"nn",

}

for i in Dictionary:
    for p in doc.paragraphs:
        if p.text.find(i)>=0:
            p.text=p.text.replace(i,Dictionary[i])
            p.alignment = 3

for para in doc.paragraphs:
    para.alignment = 3


#save changed document
doc.save('./test.docx')

Word document before running the code:-

enter image description here

Word document after running the code :-

enter image description here

"I typed return key in the place of symbol"

any method to avoid the return key symbols ?

Source: Python-3x Questions

LEAVE A COMMENT