IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Python系列之Python-docx生成运行日报Word模板 -> 正文阅读

[大数据]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
文章会同步在公众号“牧羊人的方向”更新,感兴趣的可以关注公众号,谢谢!
在这里插入图片描述

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-06-14 22:40:05  更:2022-06-14 22:41:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 22:24:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码