[Python] Add header and footer to a second DOCX only that would eventually be appended to a...

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have two DOCX files - ID-1.docx and ID-2.docx - ID-1 being a cover page and ID-2 being the content. ID-2 doesn't have a header and footer and I need the header and footer only in ID-2. Post which I merge the 2 DOCXs.

    $doc1 = escapeshellarg(DOCX_PATH."$ID-1.docx");
    $doc2 = escapeshellarg(DOCX_PATH."$ID-2.docx");
    $output = escapeshellarg(DOCX_PATH."$ID.docx");

    $cmd = PYTHON." ".ABS_PATH."merge_docs.py $doc1 $doc2 $output 2>&1";
    exec($cmd, $outputLines, $returnCode);


    How do I add header and footer to ID-2 only ? I think python's docx capabilities are bette than PhpWord ?

    merge_docx.py :

    import warnings
    warnings.filterwarnings("ignore", category=UserWarning, module="docxcompose")

    import sys
    from docx import Document
    from docxcompose.composer import Composer
    import os

    doc1_path = sys.argv[1]
    doc2_path = sys.argv[2]
    output_path = sys.argv[3]

    doc1 = Document(doc1_path)
    doc2 = Document(doc2_path)

    composer = Composer(doc1)
    composer.append(doc2)
    composer.save(output_path)

    Continue reading...

Compartilhe esta Página