示例#1
0
def make_word_document(document_title='Document title', document_filename='default_filename', content=[],
                  **doc_template_kwargs):

    if doc_template_kwargs.get('document_template'):
        doc = Document(doc_template_kwargs.get('document_template'))
    else:
        doc = Document()

    doc.core_properties.title = document_title

    if doc_template_kwargs.get('workflow_ID'):
        wf_id = doc_template_kwargs.get('workflow_ID')
        for paragraph in doc.paragraphs:
            if 'Disclaimer' in paragraph.text:
                distext = paragraph.text
                distext = distext.replace('xxxxxxxx', f'{wf_id}')
                distext = distext.replace('xx.xx.xxxx', datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
                paragraph.text = distext

    content = _set_section_levels(content)
    content = _format_document_dict(content)
    _append2worddoc(doc, content)
    doc.save(f'{document_filename}.docx')
    return doc