用python生成html

將兩張圖片排列生成html
參考:https://bbs.csdn.net/topics/20330939

//  橫向排列
<table>
   <tr>
       <td><img src="http://expert.csdn.net/images/csdn.gif"></td>
       <td><img src="http://expert.csdn.net/images/csdn.gif"></td>
   </tr>
</table>

//  縱向排列
<table>
   <tr>
       <td><img src="http://expert.csdn.net/images/csdn.gif"></td>
   </tr>
   <tr>
       <td><img src="http://expert.csdn.net/images/csdn.gif"></td>
   </tr>
</table>

生成表單存爲html
參考:https://www.cnblogs.com/jins-note/p/9520303.html

import pandas as pd
def convertToHtml(result,title):
    #將數據轉換爲html的table
    #result是list[list1,list2]這樣的結構
    #title是list結構;和result一一對應。titleList[0]對應resultList[0]這樣的一條數據對應html表格中的一列
    d = {}
    index = 0
    for t in title:
        d[t]=result[index]
        index = index+1
    df = pd.DataFrame(d)
    df = df[title]
    h = df.to_html(index=False)
    return h

 if __name__ == '__main__':
    result = [[u'2016-08-25',u'2016-08-26',u'2016-08-27'],[u'張三',u'李四',u'王二']]
    title = [u'日期',u'姓名']
    print(convertToHtml(result,title))

然後以上生成的h作爲以下的輸入

import numpy as np
import pandas as pd

HEADER = '''
<html>
    <head>
        <style>
            .df tbody tr:last-child { background-color: #FF0000; }
        </style>
    </head>
    <body>
'''
FOOTER = '''
    </body>
</html>
'''

df = pd.DataFrame({'a': np.arange(10), 'b': np.random.randn(10)})
with open('test.html', 'w') as f:
    f.write(HEADER)
    f.write(df.to_html(classes='df'))
    f.write(FOOTER)

參考:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html

完整代碼如下:

import numpy as np
import pandas as pd


HEADER = '''
<html>
    <head>
        <style>
            .df tbody tr:last-child { background-color: #FF0000; }
        </style>
    </head>
    <body>
'''
FOOTER = '''
    </body>
</html>

def convertToHtml(result , title, html_save_path):
    d = {}
    index = 0
    for t in title:
        d[t] = result[index]
        index = index + 1
    df = pd.DataFrame(d)
    df = df[title]
    h = df.to_html(index = False)

    with open(html_save_path, 'w') as f:
        f.write(HEADER)
        f.write(h)
        f.write(FOOTER)
def convertToList(list_name, case_name):
    title = []
    title.append('frame_cnt')
    title.append('gt_vel')
    title.append('rs_vel')
    title.append('diff_vel')
    title.append('gt_acc')
    title.append('rs_acc')
    title.append('diff_acc')
    vel_acc_txt = os.path.join(result_save_path, list_name, case_name, 'result/vel_acc.txt')
    vel_acc = np.loadtxt(vel_acc_txt)

    result = []
    x0 = []
    x1 = []
    x2 = []
    x3 = []
    x4 = []
    x5 = []
    x6 = []
    for i in range(vel_acc.shape[0]):
        x0.append(i+1)
        x1.append(vel_acc[i, 1])
        x2.append(vel_acc[i, 2])
        x3.append(abs(vel_acc[i, 1] - vel_acc[i, 2]))
        x4.append(vel_acc[i, 3])
        x5.append(vel_acc[i, 4])
        x6.append(abs(vel_acc[i, 3] - vel_acc[i, 4]))
    result.append(x0)
    result.append(x1)
    result.append(x2)
    result.append(x3)
    result.append(x4)
    result.append(x5)
    result.append(x6)
    html_save_path = os.path.join(result_save_path, list_name, case_name, 'html/velocity_table.html')


convertToList(list_name, case_name)    # 調用函數即可
'''

其餘參考文檔:
https://www.lizenghai.com/archives/40768.html
https://www.cnblogs.com/zenan/p/10319990.html
不錯:https://blog.csdn.net/Yellow_python/article/details/100546841

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章