I have multiple Word documents in a directory. I am using python-docx to clean them up. It's a long code, but one small part of it that you'd think would be the easiest is not working. After making some edits, I need to remove all line breaks and carriage returns. However, the following code doesn't do the job. I've tried different workarounds, such as using for loop to iterate over each character, etc. No results! However, when I tried doing it manually in Notepad++, \r was easily found and replaced.
def remove_line_breaks(document):
for paragraph in document.paragraphs:
paragraph.text = paragraph.text.replace('\r', ' ').replace('\n', ' ')