class WordUtilOld(object): def __init__(self, doc_template, replace_dict, save_path): # 模板 self.doc_template = doc_template # 替换字典 self.replace_dict = replace_dict # 保存的字典 self.save_path = save_path # self.__tpl = DocxTemplate(self.doc_template) @staticmethod def __replace_text(tpl, old_text, new_text): for p in tpl.paragraphs: if old_text in p.text: inline = p.runs for i in inline: if old_text in i.text: text = i.text.replace(old_text, new_text) i.text = text def __replace_pics(self): """替换图片""" if 'pics' in self.replace_dict: replace_pic = self.replace_dict['pics'] for each in replace_pic: # 替换 print('replace pics | {0} | {1}'.format( each, replace_pic[each])) self.__tpl.replace_media(each, replace_pic[each]) def __replace_words(self): """替换文字""" # render 要两个括号括住 if 'words' in self.replace_dict: self.__tpl.render(self.replace_dict['words']) def __replace_tables(self): """替换表格""" if 'tables' not in self.replace_dict: return replace_table = self.replace_dict['tables'] # 遍历每一个表格 for each_table in replace_table: # ---------------------------------------------------------------------------------------------------------- # 每次增加一行的模式 if each_table['mode'] == 'add_row': table = self.__tpl.tables[each_table['num']] for line_index, each_line in enumerate(each_table['value']): # 表格增加一行 row_cells = table.add_row().cells for element_index, each_element in enumerate(each_line): # 将 float 和 int 转为 str,而不是全部转为 str 防止出现 unicode 的情况 if isinstance(each_element, int) or isinstance( each_element, float): row_cells[element_index].text = str(each_element) else: row_cells[element_index].text = each_element # ---------------------------------------------------------------------------------------------------------- # 表格不变换,替换其中需要替换的内容 # FIXME 替换有问题,需要解决 elif each_table['mode'] == 'replace': # FIXME 如何才能只替换当前表格中的元素呢?如何过滤指定表格中的内容和其他部分的内容 if 'words' in self.replace_dict: self.replace_dict['words'].update(each_table['value']) else: self.replace_dict['words'] = each_table['value'] # print('table {0} : replace mode'.format(each_table['num'])) # ---------------------------------------------------------------------------------------------------------- else: print('replace table error') def __save_docx(self): """保存到文件""" self.__tpl.save(self.save_path) # print('new docx save success : {0}'.format(self.save_path)) def do_process(self): # 替换图片 self.__replace_pics() # 替换表格 self.__replace_tables() # 替换文字 self.__replace_words() # 保存 self.__save_docx()
# -*- coding: utf-8 -*- ''' Created : 2019-05-22 @author: Eric Dufresne ''' from docxtpl import DocxTemplate import io DEST_FILE = 'output/header_footer_image_file_obj.docx' tpl = DocxTemplate('templates/header_footer_image_tpl.docx') context = { 'mycompany': 'The World Wide company', } dummy_pic = io.BytesIO(open('templates/dummy_pic_for_header.png', 'rb').read()) new_image = io.BytesIO(open('templates/python.png', 'rb').read()) tpl.replace_media(dummy_pic, new_image) tpl.render(context) tpl.save(DEST_FILE)
# -*- coding: utf-8 -*- ''' Created : 2017-09-03 @author: Eric Lapouyade ''' from docxtpl import DocxTemplate DEST_FILE = 'test_files/header_footer_image.docx' tpl=DocxTemplate('test_files/header_footer_image_tpl.docx') context = { 'mycompany' : 'The World Wide company', } tpl.replace_media('test_files/dummy_pic_for_header.png','test_files/python.png') tpl.render(context) tpl.save(DEST_FILE)
# -*- coding: utf-8 -*- ''' Created : 2017-09-03 @author: Eric Lapouyade ''' from docxtpl import DocxTemplate DEST_FILE = 'output/header_footer_image.docx' tpl = DocxTemplate('templates/header_footer_image_tpl.docx') context = { 'mycompany': 'The World Wide company', } tpl.replace_media('templates/dummy_pic_for_header.png', 'templates/python.png') tpl.render(context) tpl.save(DEST_FILE)
from docxtpl import DocxTemplate import io DEST_FILE = 'output/header_footer_image.docx' tpl = DocxTemplate('templates/header_footer_image_tpl.docx') context = { 'mycompany': 'The World Wide company', } # 根据图片占位,替换图片 这种替换可以提前在模版中预设图片大小 django_pic = io.BytesIO(open('templates/django.png', 'rb').read()) dummy_pic = io.BytesIO(open('templates/dummy_pic_for_header.png', 'rb').read()) python_image = io.BytesIO(open('templates/python.png', 'rb').read()) tpl.replace_media(dummy_pic, django_pic) tpl.replace_media(django_pic, python_image) tpl.render(context) tpl.save(DEST_FILE)