i am working on creating a word file with docx python and at some point i need to surround a specific text/word from a paragraph with borders.
p6 = document.add_paragraph()
p6.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
# Add the regular text
p6.add_run("SCOLARITE Suffisante ")
# Add the special span
span = p6.add_run("OUI")
span.font.size = Pt(14)
span.font.name = 'Arial'
span.bold = True
span.font.color.rgb = RGBColor(0, 0, 0)
i want to surround the span by borders
i have tried going through the docx library but i can’t seem to find anything related to textborders
i asked chatgpt for a solution and it gave me this code
# Add borders to the second text element
for i in range(len(p6.runs)):
if i == 1:
p6.runs[i].font.underline = True
p6.runs[i].font.border = True
p6.runs[i].font.border_size = Pt(2)
p6.runs[i].font.border_color = (0, 0, 0) # Set the border color to black
which will go through each run of the paragraph(p6) and set bordres for the second run which in my case is the span. and after checking the docs i found out run.font doesn’t have a border property 🙂