Python系列之Python-docx生成运行日报Word模板

因项目需求需要自动生成运行日报,想到使用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

在这里插入图片描述

参考资料

  1. https://blog.csdn.net/yuetaope/article/details/119444970
  2. https://www.cnblogs.com/hustcser/p/8831176.html

转载请注明原文地址:https://blog.csdn.net/solihawk/article/details/125232071
文章会同步在公众号“牧羊人的方向”更新,感兴趣的可以关注公众号,谢谢!
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值