You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/27 10:01
# @Author : old tom
# @File : excel_writer.py
# @Project : py-xltpl
# @Desc : 写入excel模板
from xltpl.writerx import BookWriter
from exceltpl.pdman.pdman_reader import PDmanReader
from futool.core.fh_file_path import exist
class ATiDeDBDocWriter(object):
class TemplateNotExistError(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
def __init__(self, data, tpl_path, out_path):
"""
:param data: 数据 使用PDmanReader 读取
:param tpl_path: 模板路径
:param out_path: 输出路径
"""
if not exist(tpl_path):
raise self.TemplateNotExistError(f'[{tpl_path}]模板不存在,请检查路径')
self.writer = BookWriter(tpl_path)
self.writer.set_jinja_globals(dir=dir, getattr=getattr)
self.data = data
self.out_path = out_path
def fill(self):
payloads = []
for i, t in enumerate(self.data):
table = self.data[t]
table['sheet_name'] = str(i + 1) + '.' + table['table_name']
table['tpl_idx'] = i
payloads.append(table)
self.writer.render_sheets(payloads=payloads)
self.writer.save(self.out_path)
if __name__ == '__main__':
reader = PDmanReader(r'D:\文档\工作\ATD\2023上半年\项目\昭通OA\表结构\昭通OA.pdma.json')
writer = ATiDeDBDocWriter(reader.read_by_module('OA_CAR'), r'C:\Users\89295\Desktop\atide_db_tpl.xlsx',
r'C:\Users\89295\Desktop\车辆管理.xlsx')
writer.fill()