Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !#/usr/bin/python
- import os, sys, traceback
- def write_html_file(path, data, heads=None):
- html = []
- tab_attr = ' border="1" cellpadding="3" style="background-color:#FAFCFF; text-align:right"'
- head_attr = ' style="background-color:#C0CFE2"'
- # opening lines needed for html table
- try:
- html.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ')
- html.append('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ')
- html.append('<html xmlns="http://www.w3.org/1999/xhtml">')
- html.append('<body>')
- html.append(' <table'+tab_attr+'>')
- except:
- print 'Error setting up html heading data'
- # html table headings (if required)
- if heads:
- try:
- html.append(' <tr'+head_attr+'>')
- for item in heads:
- html.append(' '*6+'<th>'+str(item)+'</th>')
- html.append(' </tr>')
- except:
- exc_type, exc_value, exc_traceback = sys.exc_info()
- lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
- print 'Error writing html table headings:'
- print ''.join('!! ' + line for line in lines)
- # html table content
- try:
- for row in data:
- html.append(' <tr>')
- for item in row:
- html.append(' '*6+'<td>'+str(item)+'</td>')
- html.append(' </tr>')
- except:
- print 'Error writing body of html data'
- # closing lines needed
- try:
- html.append(' </table>')
- html.append('</body>')
- html.append('</html>')
- except:
- print 'Error closing html data'
- # write html data to file
- fileout = open(path, 'w')
- for line in html:
- fileout.write(line)
- print 'Data written to:', path, '\n'
- if sql_path:
- os.startfile(path)
- else:
- v_open = raw_input("Open file (Y/N):").upper()
- if v_open == 'Y':
- os.startfile(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement