View Single Post
Old 06-22-2025, 07:37 AM   #5
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 21,775
Karma: 30237628
Join Date: Mar 2012
Location: Sydney Australia
Device: none
Just for fun I asked ChatGPT, "How to Concatenate Multiple DOCX Files with File Names as Headers". Here's the response.


Quote:
To concatenate multiple DOCX files and insert each file’s name as a header before its content, you can do this manually in Microsoft Word or automate it using a script.

⚙️ Automated Method (Using Python + python-docx)
If you have many files, automation is your friend. Here’s a Python script that does the job:
Code:
from docx import Document
import os

def merge_docs_with_headers(folder_path, output_path):
    merged_doc = Document()

    for filename in sorted(os.listdir(folder_path)):
        if filename.endswith(".docx"):
            file_path = os.path.join(folder_path, filename)
            doc = Document(file_path)

            # Add file name as a heading
            merged_doc.add_heading(filename, level=1)

            # Append content
            for element in doc.element.body:
                merged_doc.element.body.append(element)

    merged_doc.save(output_path)

# Example usage
merge_docs_with_headers("path/to/your/docx/files", "merged_output.docx")

This script:
- Loops through all .docx files in a folder.
- Adds each file’s name as a heading.
- Appends the content of each file to the master document.
You’ll need to install the python-docx library if you haven’t already: pip install python-docx
BR
BetterRed is offline   Reply With Quote