Hi we are trying to change style of document, there are few issues that are seen and need support for resolving the issues
we are using the below function to change the font name and font size
def rebrand_document(self, file_path):
t0 = time.perf_counter()
base = os.path.splitext(os.path.basename(file_path))[0].replace(" ", "_")
out_dir = os.path.join("/test/",base)
os.makedirs(out_dir, exist_ok=True)
out_path = os.path.join(out_dir, f"{base}.docx")
try:
doc = aw.Document(file_path)
# Define helper to change font of runs if matches criteria
def change_font(run):
try:
font_name = (run.font.name or "").strip().lower()
if font_name in ["arial", "times new roman"]:
size = float(run.font.size)
run.font.name = self.target_font_name
if isclose(size, 12.0, abs_tol=0.01):
run.font.size = 11.0
except Exception as e:
print(e)
# Process all runs in document (deep)
try:
runs = doc.get_child_nodes(aw.NodeType.RUN, True)
run_count = runs.count
for i in range(run_count):
run = runs[i].as_run()
change_font(run)
except Exception as e:
print(e)
# Process footnotes
try:
footnotes = doc.get_child_nodes(aw.NodeType.FOOTNOTE, True)
footnote_count = footnotes.count
for i in range(footnote_count):
footnote = footnotes[i].as_footnote()
para_count = footnote.paragraphs.count
for j in range(para_count):
para = footnote.paragraphs[j]
run_count = para.runs.count
for k in range(run_count):
change_font(para.runs[k])
except Exception as e:
print(e)
# Process headers and footers
try:
section_count = doc.sections.count
for s in range(section_count):
section = doc.sections[s]
hf_count = section.headers_footers.count if section.headers_footers is not None else 0
for h in range(hf_count):
hf = section.headers_footers[h]
if hf is not None:
para_count = hf.paragraphs.count
for p in range(para_count):
para = hf.paragraphs[p]
run_count = para.runs.count
for r in range(run_count):
change_font(para.runs[r])
except Exception as e:
print(e)
# Process shapes (which may contain text)
try:
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True)
shape_count = shapes.count
for i in range(shape_count):
shape = shapes[i].as_shape()
# Use get_text_frame() method to check for text
try:
text_frame = shape.get_text_frame()
except AttributeError:
text_frame = None
if text_frame is not None:
para_count = text_frame.paragraphs.count
for j in range(para_count):
para = text_frame.paragraphs[j]
run_count = para.runs.count
for k in range(run_count):
change_font(para.runs[k])
except Exception as e:
print(e)
# Update document layout before saving
try:
doc.update_page_layout()
except Exception as e:
print(e)
try:
doc.save(out_path)
except Exception as e:
return out_path, e
t1 = time.perf_counter()
return out_path, "OK"
except Exception as e:
print(e)
issues are,
-
when we open the saved file and enter new word/sentence the text is seen in either airal /times new roman
-
the bullet points are not changing the font size/font name
-
when the font size and name are changed if any flow charts are word wrapped their location are changing
can you please help in resolution using python?