因項目需求需要自動生成運行日報,想到使用Python腳本自動生成Word運行模板,接口數(shù)據(jù)訪問elasticsearch獲取,獲取到的數(shù)據(jù)再使用pyechart生成圖表展示在Word模板中。本文主要介紹python幾種工具的安裝部署,包括python-docx、elasticsearch和pyechart環(huán)境。
1、安裝python-docx
1)官方文檔
https://python-docx.readthedocs.io/en/latest/
2)下載介質(zhì)并安裝
#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數(shù)據(jù)
#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)測試使用
舊版本調(diào)用如下:
from pyecharts import Bar
>>> from pyecharts import Bar
>>>
>>> bar=Bar("我的第一個圖表","這里是副標(biāo)題")
>>> bar.add("服裝",["襯衫","羊毛衫","褲子"],[5,20,36])
<pyecharts.charts.bar.Bar object at 0x7fb09114ebd0>
>>>
>>> bar.render()
生成的是html格式文件
新版本調(diào)用如下:
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 文件,默認(rèn)會在當(dāng)前目錄生成 render.html 文件
# 也可以傳入路徑參數(shù),如
>>> 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
參考資料文章來源:http://www.zghlxwxcb.cn/news/detail-414965.html
- https://blog.csdn.net/yuetaope/article/details/119444970
- https://www.cnblogs.com/hustcser/p/8831176.html
轉(zhuǎn)載請注明原文地址:https://blog.csdn.net/solihawk/article/details/125232071
文章會同步在公眾號“牧羊人的方向”更新,感興趣的可以關(guān)注公眾號,謝謝!文章來源地址http://www.zghlxwxcb.cn/news/detail-414965.html
到了這里,關(guān)于Python系列之Python-docx生成運行日報Word模板的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!