|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 tryhi 于 2020-12-26 01:32 编辑
- import os
- data=[['a','b','c','d'],
- ['1','2','3','4'],
- ['5','6','7','8'],
- ['9','10','11','12']]
- # 将一个二维表格转换成网页表格
- def tab_html(lst):
- html_tab = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>网页表格</title></head><body><table border="1" cellpadding="10"><tbody>'
- for i in lst:
- html_tab = html_tab + '<tr>'
- for j in i:
- html_tab = html_tab + '<td>' + j + '</td>'
- html_tab = html_tab + '</tr>'
-
- html_tab = html_tab + '</tbody></table></body></html>'
- return html_tab
- html = tab_html(data)
- with open('aa.html', mode="w",encoding='utf-8') as msg:
- msg.write(html) # 在账单日前一天写入文件
- os.system('aa.html')
复制代码
写了这样一函数用来转换,但是发现不够用了,现在我又想增加一些字体颜色,还有一些表格内的文字想要增加连接进去,感觉不好使,又得重构,有没有什么现成的函数?
刚搜到一个pyecharts模块似乎可以满足需求,不知道有没有使用方法,打个比方,我想将这样一个列表,转换为带字体颜色、背景色以及带连接的html表格,要怎么写呢?
- data=[
- [['a','0x0000FF','0x00FF00','http://www.baidu.com'],['b','0x0000FF','0x00FF00','http://www.baidu.com'],['c','0x0000FF','0x00FF00','http://www.baidu.com'],['d','0x0000FF','0x00FF00','http://www.baidu.com']],
- [['1','0x0000FF','0x00FF00','http://www.baidu.com'],['2','0x0000FF','0x00FF00','http://www.baidu.com'],['3','0x0000FF','0x00FF00','http://www.baidu.com'],['4','0x0000FF','0x00FF00','http://www.baidu.com']],
- [['5','0x0000FF','0x00FF00','http://www.baidu.com'],['6','0x0000FF','0x00FF00','http://www.baidu.com'],['7','0x0000FF','0x00FF00','http://www.baidu.com'],['8','0x0000FF','0x00FF00','http://www.baidu.com']],
- [['9','0x0000FF','0x00FF00','http://www.baidu.com'],['10','0x0000FF','0x00FF00','http://www.baidu.com'],['11','0x0000FF','0x00FF00','http://www.baidu.com'],['12','0x0000FF','0x00FF00','http://www.baidu.com']],]
复制代码
突然发现好像不用这么麻烦,反正格式都是字符串,直接传对应的html格式进去就行了
前端的东西最后由前端生成,
非要用python,那就用flask这种web构架,里面都自带了模板,传输参数就自动生成了。
如果不用模板,就只能手动。
我看你的列表里颜色和背景都是固定的,整体套用即可:
- import requests
- from lxml import etree
- def main():
- data = [
- [['a', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['b', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
- ['c', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['d', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
- [['1', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['2', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
- ['3', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['4', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
- [['5', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['6', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
- ['7', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['8', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
- [['9', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['10', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
- ['11', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
- ['12', '0x0000FF', '0x00FF00', 'http://www.baidu.com']]]
- print('<table style="color:#0000FF;background-color:#00FF00;">')
- for row_item in data:
- print('<tr>')
- for d_item in row_item:
- print(f' <td>')
- print(f' {d_item[0]}')
- print(' </td>')
- print('</tr>')
- print('</table>')
- if __name__ == '__main__':
- main()
复制代码
|
|