Skip to content

Instantly share code, notes, and snippets.

@juleskuehn
Created March 5, 2024 17:05
Show Gist options
  • Select an option

  • Save juleskuehn/93310de9259af56977bd0594bc4e49c7 to your computer and use it in GitHub Desktop.

Select an option

Save juleskuehn/93310de9259af56977bd0594bc4e49c7 to your computer and use it in GitHub Desktop.
Add header and footer to all Word documents (docx) in a directory
"""
Recursively search for .docx files in the given directory and add headers/footers to them.
By default, the script will replace the header and footer in document with the text "AI-generated content".
Arguments:
dir: The directory to search for .docx files. (positional, required)
mark_text: The text to add to the header and footer. Default is "AI-generated content". (2nd position, optional)
areas: The areas to add the mark text to. Default is ["header", "footer"]. (3rd position, optional)
Usage examples:
python mark_docx.py /path/to/directory
python mark_docx.py /path/to/directory "Confidential" header
python mark_docx.py /path/to/directory "Do not distribute" header footer
"""
import os
import sys
import argparse
from docx import Document
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
from docx.shared import RGBColor
from docx.shared import Pt
def mark_docx(docx_path, mark_text="AI-generated content", areas=["header", "footer"]):
doc = Document(docx_path)
for section in doc.sections:
for el in [getattr(section, area) for area in areas]:
if el is not None:
el.is_linked_to_previous = False
el.paragraphs[0].clear()
run = el.paragraphs[0].add_run(mark_text)
run.bold = True
run.font.color.rgb = RGBColor(128, 128, 128) # Set RGB color to grey
run.font.size = Pt(18) # Set font size to 24
el.paragraphs[0].alignment = 1
doc.save(docx_path)
def main():
parser = argparse.ArgumentParser(description="Recursively search for .docx files in the given directory and add headers/footers to them.")
parser.add_argument("dir", help="The directory to search for .docx files.")
parser.add_argument("mark_text", nargs="?", default="AI-generated content", help="The text to add to the header and/or footer. Default is 'AI-generated content'.")
parser.add_argument("areas", nargs="*", default=["header", "footer"], help="The areas to add the mark text to. Default is ['header', 'footer'].")
args = parser.parse_args()
for root, dirs, files in os.walk(args.dir):
for file in files:
if file.endswith(".docx"):
mark_docx(os.path.join(root, file), args.mark_text, args.areas)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment