0

So sometimes at my job we have to change the date in the header of dozens of word docs and print to pdf. This is such a pain and so I wrote the python code provided to go through and change text and print for me. However, the word docs have markups on them so those have to be turned off before printing. Is there a way to do this with python? I am new to python so bare with me. Hopefully my problem is clear.

import os
import docx
from docx.shared import Pt
from docx2pdf import convert

folder_path = "C:\\Users\\blablabla\\Documents\\prints\\test"
old_text = "90%"
new_text = "100%"
font_name = "Arial Narrow"
font_size = Pt(8)

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

        for section in doc.sections:
            header = section.header
            for paragraph in header.paragraphs:
                paragraph.text = paragraph.text.replace(old_text, new_text).upper()
                for run in paragraph.runs:
                    run.font.name = font_name
                    run.font.size = font_size
                    # run.text = run.text.upper()
        doc.save(file_path)
        convert(file_path)

0 Answers0