使用python生成本地html文件
2016-06-16 22:34
836 查看
一.静态HTML生成方法
#coding:utf-8 _author_ = "LiaoPan" _time_ = "2016.6.16" _myblog_ = "http://blog.csdn.net/reallocing1?viewmode=contents" f = open("demo_1.html",'w') message = """ <html> <head></head> <body> <p>Hello,World!</p> <p>demo</p> </body> </html>""" f.write(message) f.close()
#coding:utf-8 _author_ = "LiaoPan" _time_ = "2016.6.16" _myblog_ = "http://blog.csdn.net/reallocing1?viewmode=contents" import webbrowser GEN_HTML = "demo_1.html" #命名生成的html f = open(GEN_HTML,'w') message = """ <html> <head></head> <body> <p>Hello,World!</p> <p>Add webbrowser function</p> </body> </html>""" f.write(message) f.close() webbrowser.open(GEN_HTML,new = 1)
HTML涉及python变量:
#coding:utf-8 _author_ = "LiaoPan" _time_ = "2016.6.16" _myblog_ = "http://blog.csdn.net/reallocing1?viewmode=contents" import webbrowser GEN_HTML = "demo_1.html" #命名生成的html str_1 = "1: new contents need to be added." str_2 = "2: new contents need to be added." f = open(GEN_HTML,'w') message = """ <html> <head></head> <body> <p>Hello,World!</p> <p>Add webbrowser function</p> <p>%s</p> <p>%s</p> </body> </html>"""%(str_1,str_2) f.write(message) f.close() webbrowser.open(GEN_HTML,new = 1)
webbrowser.open(url, new=0, autoraise=True)
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
二.动态生成HTML
1.bottle
安装:pip install bottle
Demo:
#coding:utf-8 """ - 使用bottle来动态生成html - http://bottlepy.org/docs/dev/stpl.html """ _author_ = "LiaoPan" _time_ = "2016.6.17" _myblog_ = "http://blog.csdn.net/reallocing1?viewmode=contents" from bottle import SimpleTemplate,template #ex01 tpl = SimpleTemplate('Hello {{name}}') print tpl.render(name='World') #ex02 print template('Hello {{name}}',name = "World") #ex03 my_dict = {'Name':'L','Age':23,'Grade':"A"} print template("My name is {{Name}}, my age is {{Age}},and my grade is {{Grade}}",**my_dict)
bottle动态生成Html文件:
#coding:utf-8 """ - 使用bottle来动态生成html - https://www.reddit.com/r/learnpython/comments/2sfeg0/using_template_engine_with_python_for_generating/ """ _author_ = "LiaoPan" _time_ = "2016.6.17" from bottle import template import webbrowser #一些我们需要展示的文章题目和内容 articles = [("Title #1","Detials #1","http://blog.csdn.net/reallocing1/article/details/51694967"),("Title #2","Detials #2","http://music.163.com"),("Title #3","Detials #3","http://douban.fm")] #定义想要生成的Html的基本格式 #使用%来插入python代码 template_demo=""" <html> <head><h1>demo of bottle</h1></head> <title>Demo</title> <body> % for title,detail,link in items: <h2>{{title.strip()}}</h2> <p>{{detail}}</p> <a href={{link}}>Link text</a> %end </body </html> """ html = template(template_demo,items=articles) with open("test.html",'wb') as f: f.write(html.encode('utf-8')) #使用浏览器打开html webbrowser.open("test.html")
2.PyH
Pyh 是一个强大且简约的python模块,你可以使用它在python程序中生成HTML内容。官方网站:https://code.google.com/p/pyh/
开源地址:https://code.google.com/p/pyh/
中文文档:http://hanxiaomax.github.io/trans/pyh-chinese-doc/
下面是引用的代码示例:
python代码
from pyh import * page = PyH('My wonderful PyH page') page.addCSS('myStylesheet1.css', 'myStylesheet2.css') page.addJS('myJavascript1.js', 'myJavascript2.js') page << h1('My big title', cl='center') page << div(cl='myCSSclass1 myCSSclass2', id='myDiv1') << p('I love PyH!', id='myP1') mydiv2 = page << div(id='myDiv2') mydiv2 << h2('A smaller title') + p('Followed by a paragraph.') page << div(id='myDiv3') page.myDiv3.attributes['cl'] = 'myCSSclass3' page.myDiv3 << p('Another paragraph') page.printOut()
生成的Html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My wonderful PyH page</title> <link href="myStylesheet1.css" type="text/css" rel="stylesheet" /> <link href="myStylesheet2.css" type="text/css" rel="stylesheet" /> <script src="myJavascript1.js" type="text/javascript"></script> <script src="myJavascript2.js" type="text/javascript"></script> </head> <body> <h1 class="center">My big title</h1> <div id="myDiv1" class="myCSSclass1 myCSSclass2"> <p id="myP1">I love PyH!</p> </div> <div id="myDiv2"> <h2>A smaller title</h2> <p>Followed by a paragraph.</p> </div> <div id="myDiv3" class="myCSSclass3"> <p>Another paragraph</p> </div> </body> </html>
3.jinja2
安装:pip install Jinja2
http://jinja.pocoo.org/docs/dev/intro/#installation
基本使用方法:
>>> from jinja2 import Template >>> template = Template('Hello {{ name }}!') >>> template.render(name='John Doe') u'Hello John Doe!'
较复杂~
Ref:
- SimpleTemplate Engine of bottle
- http://bottlepy.org/docs/dev/index.html bottle官方文档
- 使用模板
- Template Designer Documentation 官方文档
- Primer on Jinja Templating
- https://www.fullstackpython.com/jinja2.html 参考学习Jinja2的资源
- https://realpython.com/blog/python/primer-on-jinja-templating/#quick-examples
- https://pythonadventures.wordpress.com/2014/02/25/jinja2-example-for-generating-a-local-file-using-a-template/ 举了个例子
- http://docs.jinkan.org/docs/jinja2/ Jinja2官方文档中文版
- http://jinja.pocoo.org/docs/dev/ Jinja官方文档
- https://www.fullstackpython.com/template-engines.html 几种动态生成HTML的方法
- https://docs.python.org/2/library/webbrowser.html 使用python打开本地html文件
- http://programminghistorian.org/lessons/creating-and-viewing-html-files-with-python
相关文章推荐
- python ssh
- python
- python类常用的内置方法
- Python内建函数与对象方法
- python SQLAlchemy
- python多版本共存之道——Pyenv
- python time
- 安装sklearn机器学习库
- Python 经典书籍
- 分析python程序运行时间的几种方法
- 利用Python实现Fibonacci Squence
- 在anaconda python开发套件下进行opencv的安装
- python数据类型详解
- python两个文件的对比
- 数据处理工具pandas
- Python学习笔记(运算符)
- python 写入文件
- 《推荐系统》基于图的推荐算法
- BinarySortTree java python leetcode
- Python中pip的安装