因项目需求需要自动生成运行日报,想到使用Python脚本自动生成Word运行模板,接口数据访问elasticsearch获取,获取到的数据再使用pyechart生成图表展示在Word模板中。本文主要介绍python几种工具的安装部署,包括python-docx、elasticsearch和pyechart环境。
1、安装python-docx
1)官方文档
https://python-docx.readthedocs.io/en/latest/
2)下载介质并安装
#tar -xzvf python-docx-0.8.11.tar.gz
#cd python-docx-0.8.11
#python setup.py install
3)测试使用
from docx import Document
from docx.shared import Inches,Cm
doc = Document()
doc.add_heading(u'测试test')
doc.add_picture("test.png",width=Inches(10))
doc.save("test.docx")
2、安装Elasticsearch接口
1)安装elasticsearch接口,能够访问elasticsearch数据
#tar -xzvf setuptools_scm-1.15.4.tar.gz
#cd setuptools_scm-1.15.4
#python setup.py install
#tar -xzvf pytest-runner-2.8.tar.gz
#cd pytest-runner-2.8
#python setup.py install
#tar -xzvf python-utils-2.3.0.tar.gz
#cd python-utils-2.3.0
#python setup.py install
#tar -xzvf urllib3-1.21.1.tar.gz
#cd urllib3-1.21.1
#python setup.py install
#pip install elasticsearch6-6.8.1-py2.py3-none-any.whl
2)直接import elasticsearch
>>> from elasticsearch6 import Elasticsearch
es = Elasticsearch(['x.x.x.x'],timeout=36000)
body1 = {"size": 10000,"query": {"match_all": {}}}
res1 = es.search(index="goods",scroll='5m', body=body1)
3、Pyechart图表工具
1)安装pyechart依赖包
# pip install backports.functools_lru_cache-1.5-py2.py3-none-any.whl
# pip install wcwidth-0.2.5-py2.py3-none-any.whl
# pip install prettytable-1.0.0-py2.py3-none-any.whl
# pip install MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
# pip install Jinja2-2.10.1-py2.py3-none-any.whl
# pip install lml-0.0.2-py2.py3-none-any.whl
# pip install pyecharts_jupyter_installer-0.0.3-py2.py3-none-any.whl
# tar -xzvf jupyter-echarts-pypkg-0.1.2.tar.gz
# cd jupyter-echarts-pypkg-0.1.2
# python setup.py install
# pip install Pillow-5.0.0-cp27-cp27mu-manylinux1_x86_64.whl
# tar -xzvf future-0.17.0.tar.gz
# cd future-0.17.0/
# python setup.py install
# pip install pyecharts_javascripthon-0.0.6-py2.py3-none-any.whl
# pip install pyecharts-0.5.11-py2.py3-none-any.whl
2)安装pyechart
# pip install pyecharts-0.5.11-py2.py3-none-any.whl
Processing ./pyecharts-0.5.11-py2.py3-none-any.whl
Requirement already satisfied: jinja2 in /usr/lib/python2.7/site-packages (from pyecharts==0.5.11) (2.10.1)
Requirement already satisfied: lml==0.0.2 in /usr/lib/python2.7/site-packages (from pyecharts==0.5.11) (0.0.2)
Requirement already satisfied: jupyter-echarts-pypkg==0.1.2 in /usr/lib/python2.7/site-packages (from pyecharts==0.5.11) (0.1.2)
Requirement already satisfied: pillow in /usr/lib64/python2.7/site-packages (from pyecharts==0.5.11) (5.0.0)
Requirement already satisfied: future in /usr/lib/python2.7/site-packages/future-0.17.0-py2.7.egg (from pyecharts==0.5.11) (0.17.0)
Requirement already satisfied: pyecharts-javascripthon==0.0.6 in /usr/lib/python2.7/site-packages (from pyecharts==0.5.11) (0.0.6)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/lib64/python2.7/site-packages (from jinja2->pyecharts==0.5.11) (1.1.1)
Requirement already satisfied: pyecharts-jupyter-installer==0.0.3 in /usr/lib/python2.7/site-packages (from jupyter-echarts-pypkg==0.1.2->pyecharts==0.5.11) (0.0.3)
Requirement already satisfied: requests; python_version < "3.5" in /usr/lib/python2.7/site-packages (from pyecharts-javascripthon==0.0.6->pyecharts==0.5.11) (2.6.0)
Installing collected packages: pyecharts
Successfully installed pyecharts-0.5.11
3)安装pyecharts_snapshot
# pip install snapshot_phantomjs-0.0.3-py2.py3-none-any.whl
# pip install pyecharts_snapshot-0.1.10-py2.py3-none-any.whl
直接使用:
# snapshot render.html test.png
No phantomjs found in your path. Please install it!
需安装phantomjs
# 直接安装包phantomjs-2.1.1-linux-x86_64
# ln -s /home/trace/checktable/package/pyechart/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin
# phantomjs
phantomjs>
4)测试使用
旧版本调用如下:
from pyecharts import Bar
>>> from pyecharts import Bar
>>>
>>> bar=Bar("我的第一个图表","这里是副标题")
>>> bar.add("服装",["衬衫","羊毛衫","裤子"],[5,20,36])
<pyecharts.charts.bar.Bar object at 0x7fb09114ebd0>
>>>
>>> bar.render()
生成的是html格式文件
新版本调用如下:
from pyecharts.charts import Bar
>>> bar = Bar()
>>> bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
<pyecharts.charts.basic_charts.bar.Bar object at 0x000001A4ACB08A58>
>>> bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
<pyecharts.charts.basic_charts.bar.Bar object at 0x000001A4ACB08A58>
>>> bar.render("mycharts.html")
'C:\\Python36\\mycharts.html'
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如
>>> bar.render()
'C:\\Python36\\render.html'
>>>
生成的是html格式文件,如果保存为png格式
>>> bar.render(path="test.png")
phantomjs version: 2.1.1
Generating file ...
File saved in /home/trace/test.png
参考资料文章来源:https://www.toymoban.com/news/detail-414965.html
- https://blog.csdn.net/yuetaope/article/details/119444970
- https://www.cnblogs.com/hustcser/p/8831176.html
转载请注明原文地址:https://blog.csdn.net/solihawk/article/details/125232071
文章会同步在公众号“牧羊人的方向”更新,感兴趣的可以关注公众号,谢谢!
文章来源地址https://www.toymoban.com/news/detail-414965.html
到了这里,关于Python系列之Python-docx生成运行日报Word模板的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!