Python Forum
How publish CSV to HTML
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How publish CSV to HTML
#1
Hi,
I use below code to come out a csv file with hyperlinks in each cell. I want to publish this as it is into HTML. Can someone please suggest if there is any fi=unction to do so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import xlsxwriter
 
# %%
import pandas as pd
input=r'D:\PythonCodes\dashinput.csv'
 
df=pd.read_csv(input)
dt=df.iloc[0:0,2]
 
     
m, n = df.values.shape
for i in range(m):
    for j in range(n):
        dt=df.values[i,j]
 
        print(dt)
     
 
 
#%%
# Create a new workbook and add a worksheet
workbook = xlsxwriter.Workbook('hyperlink.xlsx')
worksheet = workbook.add_worksheet('Hyperlinks')
 
# Format the first column
worksheet.set_column('A:A', 30)
 
# Add a sample alternative link format.
red_format = workbook.add_format({
    'font_color': 'red',
    'bold':       1,
    'underline'1,
    'font_size'12,
})
 
# Write some hyperlinks
worksheet.write_url('A1', 'http://www.python.org/'# Implicit format.
worksheet.write_url('A3', 'http://www.python.org/', string='Python Home')
worksheet.write_url('A5', 'http://www.python.org/', tip='Click here')
worksheet.write_url('A7', 'http://www.python.org/', red_format)
worksheet.write_url('A9', 'mailto:jmcnamara@cpan.org', string='Mail me')
 
dsip_item='7/12'
# Write a URL that isn't a hyperlink
worksheet.write_string('A11', 'http://www.python.org/')
worksheet.write_url('A12', r'D:/DataCheck.xlsx',string=dt)
 
workbook.close()
Reply
#2
If you have a data frame, it could be directly exported to html using .to_html method.
Reply
#3
I use as below, in my original data each cell have hyperlinks, but when use
1
df.to_html

the hyperlinks disappear. How to retain hyperlinks?
Reply
#4
(Aug-02-2019, 12:02 PM)SriMekala Wrote: the hyperlinks disappear. How to retain hyperlinks?
This is due to tag-escaping, try df.to_html(escape=False).
Reply
#5
I encounter one more issue,

1
2
3
import pandas as pd
df_hyper=pd.read_excel(r'D:\PythonCodes\hyperlink.xlsx',escape=False)
df_hyper.to_html('mylink.html',escape=False,index=False)
Still, no hyperlinks retained. I guess when I read using "pd.read_excel", the hyperlinks already disappear. Kindly help.
Reply
#6
Let we have already loaded data stored in df, e.g.

1
2
import pandas as pd
df = pd.DataFrame({'x': ['<a href="http://google.com"></a>']})
If we do df.to_html(escape=True), we get:

Output:
'<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>x</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>&lt;a href="http://google.com"&gt;&lt;/a&gt;</td>\n </tr>\n </tbody>\n</table>'
If we do df.to_html(escape=False), we get:

Output:
'<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>x</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td><a href="http://google.com"></a></td>\n </tr>\n </tbody>\n</table>'
It seems that everything works as expected.

You can also do something like this:

1
2
with open('output.html', 'wt') as f:
    df.to_html(f, escape=False)
In this case, all content will be written to the file named output.html.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020