Python + docxtpl + MongoDB快速生成word

Python + docxtpl + MongoDB快速生成word

安装所需要的环境

安装Python环境
安装MongoDB数据库
安装所需要的第三方包(pymongo,docxtpl)

实现过程及原理

查询mongdb数据库,准备好要生成word文档的数据,在word模板中进行渲染,保存生成的word

详细步骤

获取命令行参数

由于我的功能函数所需要的参数是由命令行传过来的,我需要接收命令行参数,接收代码如下,返回的参数为命令行参数的获取字典.这里我接收参数的格式为python python.py -p ‘proj_id’ -t ‘模板路径’ -v ‘version’ -r ‘run_id’

import getopt
import sys

def get_args():
    """
    获取命令行参数
    """
    arg_path = {}

    try:
        opts, args = getopt.getopt(sys.argv[1:], "p:r:v:t:", [
            "--proj_id", "--run_id", "version", "template"])
        del args
    except getopt.GetoptError:
        raise ValueError('输入参数错误')
    for name, value in opts:
        if name in ("-p", "--proj_id"):
            arg_path["proj_id"] = value
        if name in ("-r", "--run_id"):
            arg_path["run_id"] = value
        if name in ("-v", "--version"):
            arg_path["version"] = value
        if name in ("-t", "template"):
            arg_path["template"] = value
    return arg_path
利用pymongo连接MongoDB数据库

代码如下

import pymongo



def comm_db():
    """
    链接数据库
    """
    client = pymongo.MongoClient(host='127.0.0.1', port=27017)
    db = client.mydb #数据库名称mydb
    return db
  • 这里我把以上两个功能函数封装在一起当作模块导入
数据库数据的查询
  • 根据查询条件查出所需要的数据,集合为my_collections
  • 查询条件为proj_id与version,二者联合唯一
  • 代码如下
def get_proj_docx(proj_id, version):
    if version is None:
        version = ''
    file_json = comm_db()['my_collections'].find_one({"proj_id": proj_id"version": version})
    if file_json is None:
        raise Exception('数据库查询错误')
    return file_json
数据的处理

由于一些数据的计算标题的处理需要在代码中完成,这里我做了一些处理

  • 为树形结构递归创建标题
  • 计算树形结构的数据的个数
  • json序列化我需要展示的数据格式等
  • 获取当前节点的父节点
def get_tpoic(children, s):
    for index, value in enumerate(children):
        del value
        children[index]['top'] = s + '.' + str(index + 1)
        child = children[index].get('children', None)
        if children[index] and child is not None:
            get_tpoic(children[index]['children'], children[index]['top'])

def get_result(run_tree, total):
    for da in run_tree:
    	if run_info is not None:
                test_data = run_info.get('test_data', None)
                if test_data is not None:
                    run_info['test_data'] = json.dumps(test_data, indent=8)

def findparentdict(parent, node_id):
    par_get = parent.get('children', None)
    if parent is None or par_get is None:
        return None
    for it in par_get:
        if it['id'] == node_id:
            return parent
    for item in par_get:
        if it['kind'] == 'dir':
            res = findparentdict(item, node_id)
            if res:
                return res
    return None

这里只展示典型的数据处理

word模板的准备

这就就要用到jinja2的语法,类似于MVC开发模式的前端语法简单举例

{% if True %}{{‘我是小明’}}{% else %}{{ ‘我不是小明’ }}{% endif %}
模板的准备需要根据用户的需求进行自定义模板的类型,模板的语法这里让我头疼了好久,书写错误无法排错,只能慢慢查找(我没有找到好的拍错的方法)

  • 这里介绍一下模板中使用的递归函数(宏递归)
    {% macro factorial(run_tree) -%}
    {。。。。。}
    {{- factorial(run.children) -}}
    {。。。。。}
    {%- endmacro %}
    {{ factorial(run_tree[0].children) }}
  • 以下为word模板语法中的部分代码
  • 根据需求自行定义
    在这里插入图片描述
  • jinja2的语法下一期作介绍
进行模板的渲染

处理好的数据与事先准备好的模板作为参数传进来

def get_word(file, templete):
	if isinstance(file, dict):
        docx = DocxTemplate(template)
        docx.render(file)
        name = file['name'] + ".docx"
        docx.save(name) #保存在当前路径下
    else:
        raise Exception("数据格式错误")

插入图片

简单示例

	from docxtpl import DocxTemplate, InlineImage
	from docx.shared import Mm
	
	
	def getimg():
	    docx = DocxTemplate("demo.docx")
	    insert_image2 = InlineImage(docx, "qwe.png", width=Mm(140))
	    docx.render({"img": insert_image2})
	    docx.save("demo_1.docx")
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G东当

您对我的肯定,是我努力的最大动

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值