用python生成html

将两张图片排列生成html
参考:https://bbs.csdn.net/topics/20330939

//  横向排列
// 纵向排列

生成表单存为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 = '''

    
        
    
    
'''
FOOTER = '''
    

'''

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 = '''

    
        
    
    
'''
FOOTER = '''
    


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

你可能感兴趣的:(python,html)