Bottom Page

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: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()
Quote
#2
If you have a data frame, it could be directly exported to html using .to_html method.
Quote
#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?
Quote
#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).
Quote
#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.
Quote
#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.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)