How to use the docxtpl.__init__.DocxTemplate function in docxtpl

To help you get started, we’ve selected a few docxtpl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elapouya / python-docx-template / docxtpl / __init__.py View on Github external
def post_processing(self, docx_file):
        if self.crc_to_new_media or self.crc_to_new_embedded:

            if hasattr(docx_file, 'read'):
                tmp_file = io.BytesIO()
                DocxTemplate(docx_file).save(tmp_file)
                tmp_file.seek(0)
                docx_file.seek(0)
                docx_file.truncate()
                docx_file.seek(0)

            else:
                tmp_file = '%s_docxtpl_before_replace_medias' % docx_file
                os.rename(docx_file, tmp_file)

            with zipfile.ZipFile(tmp_file) as zin:
                with zipfile.ZipFile(docx_file, 'w') as zout:
                    for item in zin.infolist():
                        buf = zin.read(item.filename)
                        if ( item.filename.startswith('word/media/') and
                             item.CRC in self.crc_to_new_media ):
                            zout.writestr(item, self.crc_to_new_media[item.CRC])

docxtpl

Python docx template engine

LGPL-2.1
Latest version published 1 month ago

Package Health Score

79 / 100
Full package analysis