After searching and replacing text the text font size of the replaced text is set to 12pt
See original GitHub issueI am using docx and regex to find and substitute certain keywords from the word doc. The finding and substituting is working fine. When the word is replaced it is set to a much smaller font than before, sometimes. This behavior is inconsistent - on some sections the text is replaced with the correct (matching previous text) font size.
I have not been able to find anything on this topic. Can anyone explain if this is expected behavior and if so how I can correct it?
I have tried inspecting the font size of the text before and after the substitution. According to the logs they are the same size but in the actual saved document they are not.
all fonts = Times New Roman
Original [docx] File:
- [name, city, state, date] :: [font size]
- [26, 12, 12, 13]
Saved [docx] File:
- [12, 12, 12, 12]
Logs from Python:
'after: \t\t Pat : 152400',
'before: courseCity, courseState : None',
'after: San Francisco, courseState : None',
'before: San Francisco, courseState : None',
'after: San Francisco, CA : None',
'before: \t\t\t courseDate : 152400',
'after: \t\t\t June 39, 2023 : 152400',
and the code itself (log points @ line 25 and 27):
#--- input from server ---#
task = sys.argv[1]
// parse as JSON in python to enable dict-like action on the student objects contained in the students array
students = json.loads(sys.argv[2])
#--- current date ---#
currentDate = time.strftime("%B %d, %Y")
## -------------- CORE FUNCTIONS ------------ ##
def findAndReplace(student, body):
if 'currentDate' in body.text:
find = re.compile("currentDate")
body.text = find.sub(currentDate, body.text)
for studentInfo in student:
if studentInfo in body.text:
find = re.compile(studentInfo)
print('before: ' + str(body.text) + ' : ' + str(body.style.font.size))
body.text = find.sub(student[studentInfo], body.text)
print('after: ' + str(body.text) + ' : ' + str(body.style.font.size))
def newDoc(task):
for student in students:
# open a new template document for each student to prevent overlap
document = docx.Document("./AutomationTemplates/" + task + "_template.docx")
# check paragraph text
for paragraph in document.paragraphs:
findAndReplace(student, paragraph)
# check table cells
for table in document.tables:
for row in table.rows:
for cell in row.cells:
findAndReplace(student, cell)
# save the document when finished
document.save("./AutomationResults/"+ task + "/" + student["studentName"] + "_" + task + ".docx")
print(task + "s created in /AutomationResults/" + task)
## ------ CALL THE NEW DOC PASSING THE AUTOMATION TASK -------- ##
newDoc(task)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Replace font size not working in Word 2010
I am trying to replace all non-standard font sizes in a document with organization-standard font sizes. Change 9pt to 10pt.
Read more >Word Find and Replace Formatting - CustomGuide
1. Click the Replace button on the Home tab. 2. Click More to expand the dialog box. 3. Click the Format button.
Read more >Word 2016: Formatting Text - GCF Global
Format text to improve Word documents. Learn how to change the font, change font size, change font color, and change text alignment.
Read more >Type properties panel bug - Illustrator UserVoice
Every time when changing a text's properties and deselecting it ... Workaround 1)After Creating Second text asset first change the font size ...
Read more >How to Find and Replace Formatting in Microsoft Word
Please watch the updated version of this tutorial: https://youtu.be/S1ZBErjrGuU. Learn how to find and replace formatting in Microsoft Word.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had a similar issue when trying to substitute parts of text found via regex. My template document was font size 10, but the somehow the replaced text got set to font size 11.
To preserve the original size, here is an adapted solution pertaining to paragraphs (not tables as seen above). While iterating
document.paragraphs
, removeparagraph.text = text
and call this function in the loop.Note, I have not tested this extensively. @scanny Thank you. Your code saved the day.
This function can reliably replace a certain word or phrase with another in a paragraph, retaining the formatting of the original word: https://github.com/python-openxml/python-docx/issues/30#issuecomment-879593691