How publish CSV to HTML - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: How publish CSV to HTML (/thread-20229.html) |
How publish CSV to HTML - SriMekala - Aug-01-2019 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() RE: How publish CSV to HTML - scidam - Aug-01-2019 If you have a data frame, it could be directly exported to html using .to_html method.
RE: How publish CSV to HTML - SriMekala - Aug-02-2019 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? RE: How publish CSV to HTML - scidam - Aug-02-2019 (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) .
RE: How publish CSV to HTML - SriMekala - Aug-03-2019 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. RE: How publish CSV to HTML - scidam - Aug-04-2019 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: If we do df.to_html(escape=False) , we get: 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 .
|