0
new_phrase = open("d.txt").read()

with open("phrase.txt", "r") as f:
    words=f.read().splitlines()

for word in words:
    new_phrase = new_phrase.replace(word, f'<span style="color:Green;">{word}</span>')
    
with open("my.html","w") as fp:
   fp.write(new_phrase)

input("Press Enter to continue...")

How to retain paragraph break in html output. Now the original content containing three paragraphs is grouped into one big paragraph when I export it as html.

Output for print(new_phrase)

On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, <span style="color:Green;">pore</span> footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your <span style="color:Green;">current</span> document look.
You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the <span style="color:Green;">current</span> theme or using a format that you specify directly.
To change the overall look of your document, choose new Theme elements <span style="color:Green;">pour</span> on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your <span style="color:Green;">current</span> template.

HTMl output current

enter image description here

1
  • You may want to peek at your html file using a text editor to see what you're outputting. Commented Sep 20, 2020 at 12:10

1 Answer 1

2

The splitlines() method strips newline characters by default. You can pass in the keyword argument keepends to keep the line endings:

>>> with open('test.txt') as f:
...     print(f.read().splitlines(keepends=True))
...
['line 1 \n', '\n', 'line 3\n']

You could also use the readlines() method on the file object itself:

>>> with open('test.txt') as f:
...     print(f.readlines())
...
['line 1 \n', '\n', 'line 3\n']
Sign up to request clarification or add additional context in comments.

4 Comments

Can you do that for html output? cause when i print(newphrase) it is indented only when passed through as html output it groups together.
Would you be willing to share d.txt and and phrase.txt? It would be easier to understand the problem if we could see them. As far as Python is concerned, this is not HTML, it is just text, so it should work
It is similar sounding words i am trying to search and find and color it for second opinion, my plan was to move it to html and then to docx to edit. Unsure how to upload txt files so copy pasted. inconvenience regretted.
I misunderstood your original question - I thought the issue was that Python was removing the newlines, but I think you are just trying to replace them with HTML line breaks. To do that, you just need to call new_phrase.replace('\n', '<br />') (only works if you retain the newlines as described in my original answer) or f'{line}<br />' for line in lines on a list of lines, which you could do when you read the file: lines = f'{line}<br /> for line in f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.