Search code examples
pythonpython-3.xpython-docx

How to add a page border with python-docx


I want to add a page border to my MS Word Document using python-docx. How can I do ?

I check the section's documentation but I didn't find anything about this.


Solution

  • Still not supported by the official python-docx library, but you can implement it yourself. you're looking for page borders stored under section properties.

    Here's some code sample that should help you out:

    import docx
    from docx.oxml.xmlchemy import OxmlElement
    from docx.oxml.shared import qn
    
    doc = docx.Document()
    sec_pr = doc.sections[0]._sectPr # get the section properties el
    # create new borders el
    pg_borders = OxmlElement('w:pgBorders')
    # specifies how the relative positioning of the borders should be calculated
    pg_borders.set(qn('w:offsetFrom'), 'page')
    for border_name in ('top', 'left', 'bottom', 'right',): # set all borders
        border_el = OxmlElement(f'w:{border_name}')
        border_el.set(qn('w:val'), 'single') # a single line
        border_el.set(qn('w:sz'), '4') # for meaning of  remaining attrs please look docs
        border_el.set(qn('w:space'), '24')
        border_el.set(qn('w:color'), 'auto')
        pg_borders.append(border_el) # register single border to border el
    sec_pr.append(pg_borders) # apply border changes to section
    
    doc.save('border_test.docx')