0
点赞
收藏
分享

微信扫一扫

DTCloud 下载数据至word模板

Spinach菠菜 2022-04-26 阅读 85
python
import base64
from datetime import datetime
from io import BytesIO
from docxtpl import DocxTemplate

gender_dict = {
    'female': '女',
    'male': '男',
    False: ''
}

pol_dict = {
    'pol_01': '中共党员',
    'pol_02': '中共预备党员',
    'pol_03': '共青团员',
    'pol_04': '民革会员',
    'pol_05': '民盟盟员',
    'pol_06': '民建会员',
    'pol_07': '民进会员',
    'pol_08': '农工党党员',
    'pol_09': '致公党党员',
    'pol_10': '九三学社社员',
    'pol_11': '台盟盟员',
    'pol_12': '无党派民主人士',
    'pol_13': '群众',
    'pol_14': '其他',
    False: ''
}
    
    def save_docs_temp(self):
        """根据当前系统模板,自动生成word文档
          # doc.add_picture("E:\\HD_Settled\\dtcloud360\\gz2.jpg")
        """
        doc_path = self.env['ir.config_parameter'].get_param('docx_template_path')
        doc = DocxTemplate(doc_path)  # 模板文档
        context = {'name': self.name or '', 'sex': gender_dict[self.gender],
                   'age': self.age or '', 'nation': self.base_nation.name,
                   'birtd': self.date_birthday or '', 'company_name': self.company_id.name or '',
                   'work_data': self.participate_time or '', 'join_data': self.join_company_time or '',
                   'pol_outlook': pol_dict[self.pol_outlook], 'education': self.education or '',
                   'major_studied': self.major_studied or '',
                   'talent_level': self.talent_level or '', 'graduation_school': self.graduation_school or '',
                   'job_education': self.job_education or '', 'job_major': self.job_graduation_school or '',
                   'specific_specialty': self.specific_specialty or '', 'degree_education': self.degree_education or '',
                   'talent_professional_name': self.talent_professional_id.name or '',
                   'talent_skill_name': self.talent_skill_job_id.name or '',
                   'job_name': self.job_name or '', 'rank_level': self.rank_level or '',
                   'talent_secret': self.talent_secret or '', 'other_professional': self.other_professional or '',
                   'talent_title': self.talent_title or ''
                   }  # 待替换对象
        doc.render(context)  # 执行替换
        # 插入图片操作
        buffer = BytesIO()
        doc.save(buffer)
        return base64.encodebytes(buffer.getvalue())

    def btn_doc_method(self):
        """下载docx"""
        context = dict(self._context or {})
        active_ids = context.get('active_ids', []) or []
        wiz_obj = self.env['talent.export.export.data.wizard']
        filename = '人员信息表%s-%s' % (self.name, datetime.today().strftime('%Y-%m-%d'))
        wiz_id = wiz_obj.sudo().create({
            'file_data': self.save_docs_temp()
        })
        value = dict(
            type='ir.actions.act_url',
            target='self',
            url='/web/content?model=%s&id=%s&field=file_data&download=true&filename=%s.docx' % (
                'talent.export.export.data.wizard', wiz_id.id, filename),
        )
        return value
# -*- coding: utf-8 -*-
from dtcloud import models, fields, api


class TalentExportWizard(models.TransientModel):
    _name = 'talent.export.export.data.wizard'
    _description = '导出模板'

    file_data = fields.Binary(u'导出文件')
                                                       中亿丰——何双新
举报

相关推荐

0 条评论