Need to convert a document from TXT to HTML format programmatically? With Aspose.Words for Python via .NET any developer can convert TXT to HTML format with just a few lines of Python code. Modern document-processing Python API creates HTML from a TXT document with high speed. Test the quality of TXT to HTML conversion right in your browser. Powerful Python library allows converting TXT files to many popular formats. Save TXT as HTML in PythonThe
following example demonstrates how to convert a TXT document to HTML in Python. Follow the easy steps to turn a TXT document into HTML format. Read your TXT file from the local drive, then simply save it in web document format, specifying the required file format by HTML extension. For both TXT reading and HTML writing you can use fully qualified filenames. The output HTML content and formatting will be identical to the original TXT document. How to convert TXT
to HTML- Install 'Aspose.Words for Python via .NET'.
- Add a library reference (import the library) to your Python project.
- Open the source TXT file in Python.
- Call the 'save()' method, passing an output filename with HTML extension.
- Get the result of TXT conversion as HTML.
Python library to convert TXT to HTMLWe host our Python packages in
PyPi repositories. Please follow the step-by-step instructions on how to install "Aspose.Words for Python via .NET" to your developer environment. System RequirementsThis package is compatible with Python 3.5, 3.6, 3.7, 3.8 and 3.9. If you develop software for Linux,
please have a look at additional requirements for gcc and libpython in Product Documentation. I have a text file that contains : JavaScript 0
/AA 0
OpenAction 1
AcroForm 0
JBIG2Decode 0
RichMedia 0
Launch 0
Colors>2^24 0
uri 0
I wrote this code to convert the text file to html : contents = open("C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
for lines in contents.readlines():
e.write(lines + "<br>\n")
but the problem that I had in html file that in each line there is no space between the two columns: JavaScript 0
/AA 0
OpenAction 1
AcroForm 0
JBIG2Decode 0
RichMedia 0
Launch 0
Colors>2^24 0
uri 0
what should I do to have the same content and the two columns like in text file
asked Jul 12, 2014 at 16:34 2 Just change your code to include <pre> and </pre> tags to ensure that your text stays formatted the way you have formatted it in your original text file. contents = open"C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
for lines in contents.readlines():
e.write("<pre>" + lines + "</pre> <br>\n")
answered Jul 12, 2014 at 16:45 雨が好きな人雨が好きな人 4213 silver badges14 bronze
badges 3 This is HTML -- use BeautifulSoup from bs4 import BeautifulSoup
soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)
with open('path/to/input/file.txt') as infile:
for line in infile:
row = soup.new_tag('tr')
col1, col2 = line.split()
for coltext in (col2, col1): # important that you reverse order
col = soup.new_tag('td')
col.string = coltext
row.insert(0, col)
table.insert(len(table.contents), row)
with open('path/to/output/file.html', 'w') as outfile:
outfile.write(soup.prettify())
answered Jul 12, 2014 at
17:26 Adam SmithAdam Smith 49.5k11 gold badges70 silver badges108 bronze badges That is because HTML parsers collapse all
whitespace. There are two ways you could do it (well probably many more). One would be to flag it as "preformatted text" by putting it in <pre>...</pre> tags. The other would be a table (and this is what a table is made for): <table>
<tr><td>Javascript</td><td>0</td></tr>
...
</table>
Fairly tedious to type out by hand, but easy to generate from your script. Something like this should work: contents = open("C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
e.write("<table>\n")
for lines in contents.readlines():
e.write("<tr><td>%s</td><td>%s</td></tr>\n"%lines.split())
e.write("</table>\n")
answered Jul 12, 2014 at 16:40
neilneil 3,1671 gold
badge13 silver badges11 bronze badges 0 You can use a standalone template library like
mako or jinja . Here is an example with jinja: from jinja2 import Template
c = '''<!doctype html>
<html>
<head>
<title>My Title</title>
</head>
<body>
<table>
<thead>
<tr><th>Col 1</th><th>Col 2</th></tr>
</thead>
<tbody>
{% for col1, col2 in lines %}
<tr><td>{{ col 1}}</td><td>{{ col2 }}</td></tr>
{% endfor %}
</tbody>
</table>
</body>
</html>'''
t = Template(c)
lines = []
with open('yourfile.txt', 'r') as f:
for line in f:
lines.append(line.split())
with open('results.html', 'w') as f:
f.write(t.render(lines=lines))
If you can't install jinja , then here is an alternative: header = '<!doctyle html><html><head><title>My Title</title></head><body>'
body = '<table><thead><tr><th>Col 1</th><th>Col 2</th></tr>'
footer = '</table></body></html>'
with open('input.txt', 'r') as input, open('output.html', 'w') as output:
output.writeln(header)
output.writeln(body)
for line in input:
col1, col2 = line.rstrip().split()
output.write('<tr><td>{}</td><td>{}</td></tr>\n'.format(col1, col2))
output.write(footer)
answered Jul 12, 2014 at 17:03 Burhan KhalidBurhan Khalid 164k18 gold badges238 silver
badges276 bronze badges I have added title, looping here line by line and appending each line on < tr > and < td > tags, it is should work as single table without
column. No need to use these tags(< tr >< /tr > and < td >< /td >[gave a spaces for readability]) for col1 and col2. log: snippet: MUTHU PAGE 2019/08/19 19:59:25 MUTHUKUMAR_TIME_DATE,line: 118 INFO | Logger object created for: MUTHUKUMAR_APP_USER_SIGNUP_LOG 2019/08/19 19:59:25 MUTHUKUMAR_DB_USER_SIGN_UP,line: 48 INFO | ***** User SIGNUP page start ***** 2019/08/19 19:59:25 MUTHUKUMAR_DB_USER_SIGN_UP,line: 49 INFO | Enter
first name: [Alphabet character only allowed, minimum 3 character to maximum 20 chracter]
html source page: ''' <?xml version="1.0" encoding="utf-8"?>
<body>
<table>
<p>
MUTHU PAGE
</p>
<tr>
<td>
2019/08/19 19:59:25 MUTHUKUMAR_TIME_DATE,line: 118 INFO | Logger object created for: MUTHUKUMAR_APP_USER_SIGNUP_LOG
</td>
</tr>
<tr>
<td>
2019/08/19 19:59:25 MUTHUKUMAR_DB_USER_SIGN_UP,line: 48 INFO | ***** User SIGNUP page start *****
</td>
</tr>
<tr>
<td>
2019/08/19 19:59:25 MUTHUKUMAR_DB_USER_SIGN_UP,line: 49 INFO | Enter first name: [Alphabet character only allowed, minimum 3 character to maximum 20 chracter]
''' CODE: from bs4 import BeautifulSoup
soup = BeautifulSoup(features='xml')
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)
with open('C:\\Users\xxxxx\\Documents\\Latest_24_may_2019\\New_27_jun_2019\\DB\\log\\input.txt') as infile:
title_s = soup.new_tag('p')
title_s.string = " MUTHU PAGE "
table.insert(0, title_s)
for line in infile:
row = soup.new_tag('tr')
col1 = list(line.split('\n'))
col1 = [ each for each in col1 if each != '']
for coltext in col1:
col = soup.new_tag('td')
col.string = coltext
row.insert(0, col)
table.insert(len(table.contents), row)
with open('C:\\Users\xxxx\\Documents\\Latest_24_may_2019\\New_27_jun_2019\\DB\\log\\output.html', 'w') as outfile:
outfile.write(soup.prettify())
answered Aug 21, 2019 at 16:06
How do I convert text to HTML?
You can change a plain text message to HTML.. In the message, click Reply, Reply All, or Forward.. If you're working in the Reading Pane, click Pop Out. Note: If you aren't working in the Reading Pane, you don't need to do this.. In the message window, click Format Text > HTML..
Can Python output to HTML?
Python language has great uses today in almost every field, it can be used along with other technologies to make our lives easier. One such use of python is getting the data output in an HTML file. We can save any amount of our input data into an HTML file in python using the following examples in two ways.
|