Contracts, tender documents, and court filings usually have fixed expectations about the page itself: a frame around the text, a number on every line so clauses can be cited, and extra space on one side for binding. None of that is hard to click through in Word, but a single file often contains dozens of sections that all need to match, and a batch job would mean repeating the same clicks hundreds of times.
Page borders, line numbers, and gutters all belong to the same family of settings — section-level page setup. Once written into the file, they produce exactly what a person would get by hand, which makes them a good fit for document generation. This article uses Spire.Doc for Python to cover all three: a single style for the whole frame and per-side styling, line numbering with a chosen start value and interval, and gutter width and position, plus a few details that only show up when the settings are written to disk.
Setting Up the Environment
Install Spire.Doc and import the modules:
pip install Spire.Doc from spire.doc import * from spire.doc.common import * All three settings live on a section and are reached through document.Sections[i].PageSetup. A document with several sections needs the settings applied to each one.
Adding a Page Border to the Whole Document
The straightforward approach is to give all four sides the same style:
from spire.doc import * from spire.doc.common import * document = Document() section = document.AddSection() paragraph = section.AddParagraph() paragraph.AppendText("The frame around this page is defined by the page border settings.") # Add page borders with a special style and colour. borders = document.Sections[0].PageSetup.Borders borders.BorderType = BorderStyle.DoubleWave borders.Color = Color.get_LightSeaGreen() # Set the space between the border and the text (0-31 points). borders.Left.Space = 24 borders.Right.Space = 24 document.SaveToFile("PageBorders.docx", FileFormat.Docx2013) document.Close() BorderType applies to all four sides at once and Color changes them together; individual sides can then be adjusted afterwards.
Two things tend to trip people up. First, BorderType and Color are writable properties in the current version, so they take an assignment; calling them like methods (BorderType(BorderStyle.DoubleWave)) raises a TypeError. Second, Space is the distance between the border and the body text, measured in points, and it only accepts values from 0 to 31 — passing 50 throws an Arg_ArgumentOutOfRangeException.
Styling Each Side Separately
Formal documents often want a heavy top rule, a double rule at the bottom, and light dotted sides. Each side carries its own set of properties:
from spire.doc import * from spire.doc.common import * document = Document() section = document.AddSection() paragraph = section.AddParagraph() paragraph.AppendText("Only the top and bottom of this page are framed.") borders = document.Sections[0].PageSetup.Borders # Style the four sides one by one. borders.Top.BorderType = BorderStyle.Thick borders.Top.Color = Color.get_Red() borders.Top.LineWidth = 2.0 borders.Top.Space = 24 borders.Bottom.BorderType = BorderStyle.Double borders.Bottom.Color = Color.get_Blue() borders.Bottom.LineWidth = 1.5 borders.Bottom.Space = 24 borders.Left.BorderType = BorderStyle.Dot borders.Left.Color = Color.get_Gray() borders.Left.LineWidth = 1.0 borders.Right.BorderType = BorderStyle.Dot borders.Right.Color = Color.get_Gray() borders.Right.LineWidth = 1.0 # Measure the border from the text edge instead of the paper edge. document.Sections[0].PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.Text # Draw the frame on every page of the section. document.Sections[0].PageSetup.PageBordersApplyType = PageBordersApplyType.AllPages document.SaveToFile("PageBordersSides.docx", FileFormat.Docx2013) document.Close() Each side exposes BorderType, Color, LineWidth, and Space. LineWidth is measured in points: a value of 2.0 is stored as 16 because the file format counts eighths of a point.
Three section-level switches are worth knowing:
-
PageBorderOffsetFromdecides what the border is measured from. The default is the paper edge (PageEdge); switching toTextanchors it to the body text, and the two produce visibly different results. -
PageBordersApplyTypecontrols where the frame appears:AllPages,FirstPage, andAllExceptFirstPagecover every page, the first page only, or everything but the first page. -
IsFrontPageBorderis easy to misread. It does not mean "show a border on the first page" — it controls the stacking order, and setting it toTruedraws the border in front of the text.
To remove an existing frame, set the style back to BorderStyle.none; reading borders.NoBorder then returns True.
Adding Line Numbers
Line numbers give every clause a reference that can be cited in correspondence and disputes:
from spire.doc import * from spire.doc.common import * document = Document() section = document.AddSection() # Enough rendered lines for the interval to actually show a number. for i in range(1, 15): paragraph = section.AddParagraph() paragraph.AppendText(f"Clause {i}: the parties agree to the following terms and conditions.") pageSetup = document.Sections[0].PageSetup # Start counting from 1. pageSetup.LineNumberingStartValue = 1 # Only every sixth line is numbered. pageSetup.LineNumberingStep = 6 # Distance between the number and the text, in points. pageSetup.LineNumberingDistanceFromText = 40 # Keep counting across pages; the other modes are RestartPage and RestartSection. pageSetup.LineNumberingRestartMode = LineNumberingRestartMode.Continuous document.SaveToFile("LineNumbers.docx", FileFormat.Docx2013) document.Close() Each parameter covers one decision:
-
LineNumberingStartValueis the number given to the first counted line. The file stores a zero-based offset, so a setting of 1 is written as 0 and a setting of 5 as 4 — do not read the stored value back as the line number itself. -
LineNumberingStepis the interval: 6 means lines 6, 12, 18, and so on carry a number. There is a trap here worth dwelling on: the count runs over the rendered lines, and if the interval is larger than the total number of lines, no number appears at all — a three-line document with an interval of 6 stores the settings perfectly and still shows nothing when opened. That is why the example deliberately produces fourteen lines; set the step to 1 to number every line. -
LineNumberingDistanceFromTextis the gap between the number and the text, in points; 40 points is stored as 800 twips. -
LineNumberingRestartModedecides whether the count restarts:Continuousruns across pages,RestartPagebegins again on each page, andRestartSectionbegins again in each section.
Adding a Gutter
A gutter is extra space reserved along one edge of the page so the document can be punched and bound:
from spire.doc import * from spire.doc.common import * document = Document() section = document.AddSection() for i in range(1, 9): paragraph = section.AddParagraph() paragraph.AppendText(f"Clause {i}: the parties agree to the following terms and conditions.") pageSetup = document.Sections[0].PageSetup # Reserve a binding margin, in points. pageSetup.Gutter = 100 # Put the binding margin at the top of the page. pageSetup.IsTopGutter = True # Combine the gutter with a page frame and line numbers. borders = pageSetup.Borders borders.BorderType = BorderStyle.Single borders.Color = Color.get_Black() borders.Top.Space = 24 borders.Bottom.Space = 24 pageSetup.LineNumberingStartValue = 1 pageSetup.LineNumberingStep = 1 pageSetup.LineNumberingDistanceFromText = 24 pageSetup.LineNumberingRestartMode = LineNumberingRestartMode.RestartPage document.SaveToFile("PageGutter.docx", FileFormat.Docx2013) document.Close() Gutter is measured in points as well: 100 points is recorded as 2000 twips. IsTopGutter = True moves the binding margin to the top of the page, which suits sideways binding, and it is written into the document settings part.
The example combines all three settings: a plain black frame, line numbering that restarts on every page, and a binding margin. Combinations like this are typical of documents that will be archived.
Practical Tips
- All three are section-level settings: apply them to every section, and iterate over
document.Sectionsrather than assuming there is only one. - Use assignment syntax:
BorderType,Color,LineWidth, andSpaceare properties, and calling them like methods raises aTypeError. - Keep the spacing within 31 points: a
Spaceoutside the 0–31 range throws anArg_ArgumentOutOfRangeException, which is the most common failure here. - The line-number start value is an offset: setting 1 stores 0, so pass the number you actually want to see.
- Keep the interval below the real line count: a document with an interval of 6 needs at least six rendered lines, otherwise the file opens with no numbers visible.
- Plan the gutter and the frame together: both eat into the margin, so a wide gutter needs the page margins adjusted alongside it.
- Clear a frame with
BorderStyle.none: afterwardsborders.NoBorderreads back asTrue, which is handy for a conditional check. - Watch the scope of
PageBordersApplyType: it applies to the current section only, so a first page that needs its own frame has to be configured section by section.
Conclusion
Page borders, line numbers, and gutters are the three page-level settings that formal documents ask for most often and that people hand-configure most slowly. With Spire.Doc for Python they all belong to the section's PageSetup: Borders controls the style, colour, width, and spacing of the frame, LineNumberingStartValue, LineNumberingStep, LineNumberingDistanceFromText, and LineNumberingRestartMode control the numbering, and Gutter with IsTopGutter reserves the binding edge.
Turn those parameters into one configuration object in a template and the same layout rules can be applied to every document a script produces — from a several-hundred-page tender to a short contract that needs clause numbering.





