import os import win32com.client as win32 import math print("imported Python libraries") word_app = win32.gencache.EnsureDispatch('Word.Application') # Iterate over all open documents print("Iterate over all open documents") for doc in word_app.Documents: for paragraph in doc.Paragraphs: for sentence in paragraph.Range.Sentences: for word in sentence.Words: text = word.Text.rstrip() # Remove trailing spaces half = math.ceil(len(text) / 2) start = word.Start mid = start + half end = word.End # Create Range objects for each part of the word #print("Create Range objects for each part of the word") first_half = doc.Range(start, mid) second_half = doc.Range(mid, end) # Apply formatting to each Range #print("Apply formatting to each Range") first_half.Bold = True second_half.Bold = False # Change the font and size for the entire paragraph paragraph.Range.Font.Name = 'Calibri (Body)' paragraph.Range.Font.Size = 12 # Change the page color of the document doc.PageColor = win32.constants.wdColorLightYellow # Save the modified document in the original document's location with a new name print("Save the modified document in the original document's location with a new name") doc_dir = os.path.dirname(doc.FullName) doc_name = os.path.basename(doc.FullName) new_doc_name = 'bionic_enabled_' + doc_name new_doc_path = os.path.join(doc_dir, new_doc_name) doc.SaveAs(new_doc_path) print("conversion is completed") word_app.Quit()