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.

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:[email protected]', 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
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,

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.

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:

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