
In everyday work, Excel files are frequently printed — reports, inventories, invoices, and more. However, without proper page layout settings, printouts often suffer from truncated content, uneven margins, missing header rows, and other issues that undermine the document's professionalism and readability.
By programmatically configuring Excel page layout parameters with Python, you can apply print-ready settings at the moment the file is generated, eliminating tedious manual adjustments. This article walks through how to set margins, paper size, page orientation, print area, print title rows/columns, and fit-to-page scaling using Spire.XLS for Python.
Prerequisites
Install the required Python package:
pip install Spire.XLS
Setting Page Margins
Page margins define the distance between the printed content and the edges of the paper. Proper margins make printouts look cleaner and more readable.
You can set the top, bottom, left, and right margins independently through the TopMargin, BottomMargin, LeftMargin, and RightMargin properties of the PageSetup object. Values are specified in inches.
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
pageSetup = sheet.PageSetup
# Set page margins (in inches)
pageSetup.TopMargin = 3
pageSetup.BottomMargin = 2
pageSetup.LeftMargin = 1
pageSetup.RightMargin = 1
workbook.SaveToFile("SetMargins.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
The code above sets the top margin to 3 inches, the bottom margin to 2 inches, and both the left and right margins to 1 inch. Adjust these values as needed to suit your print requirements.
Setting Page Orientation
When a worksheet contains many columns, landscape orientation is usually a better choice than portrait, as it prevents content from exceeding the paper width.
You can switch the page orientation via the PageSetup.Orientation property:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
# Set page orientation to landscape
sheet.PageSetup.Orientation = PageOrientationType.Landscape
workbook.SaveToFile("SetOrientation.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
The PageOrientationType enumeration provides two options: Portrait (vertical) and Landscape (horizontal). By default, Excel uses portrait orientation.
Setting Paper Size
Different printing scenarios may call for different paper sizes. For instance, everyday office documents typically use Letter or A4, while large reports may require A3.
You can specify the paper size through the PageSetup.PaperSize property:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
sheet = workbook.Worksheets[0]
# Set paper size to A4
sheet.PageSetup.PaperSize = PaperSizeType.PaperA4
workbook.SaveToFile("SetPaperSize.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
The PaperSizeType enumeration supports a wide range of standard paper sizes, including PaperA3, PaperA4, PaperA5, PaperLetter, PaperLegal, and more. Choose the one that matches your printer's supported paper types.
Setting the Print Area
When a worksheet contains a large amount of data but only a portion of it needs to be printed, you can define a print area to specify exactly which cells should appear on the printout.
Use the PageSetup.PrintArea property to specify the print range as a cell address string:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
pageSetup = sheet.PageSetup
# Set the print area to A1:E5
pageSetup.PrintArea = "A1:E5"
workbook.SaveToFile("SetPrintArea.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Once a print area is defined, Excel will only print the content within that range, ignoring everything outside it. This is especially useful when working with large datasets.
Setting Print Title Rows and Title Columns
When data spans multiple printed pages, header rows typically appear only on the first page, leaving subsequent pages without column headings and making the content harder to follow. By setting Print Title Rows, you can have specific rows automatically repeated at the top of every printed page.
Similarly, when a worksheet has many columns and prints across multiple pages horizontally, you can set Print Title Columns to repeat specific columns on every page.
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
pageSetup = sheet.PageSetup
# Set rows 1 and 2 as print title rows (repeated on every page)
pageSetup.PrintTitleRows = "$1:$2"
# Set columns A and B as print title columns (repeated on every page)
pageSetup.PrintTitleColumns = "$A:$B"
workbook.SaveToFile("SetPrintTitles.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Note that title row and column references use the $ notation — for example, $1:$2 refers to rows 1 through 2, and $A:$B refers to columns A through B. This is consistent with Excel's absolute reference syntax.
Scaling to Fit the Page
When content exceeds a single page, you can use fit-to-page settings to automatically scale it across a specified number of pages. This approach is more convenient than manually adjusting the zoom percentage.
Use the FitToPagesTall and FitToPagesWide properties to control how many pages the content should span vertically and horizontally:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
pageSetup = sheet.PageSetup
# Fit all content to 1 page tall and 1 page wide
pageSetup.FitToPagesTall = 1
pageSetup.FitToPagesWide = 1
workbook.SaveToFile("FitToPage.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Setting both properties to 1 shrinks all content onto a single page. If the worksheet has many rows but few columns, you can set FitToPagesTall to a larger value or to 0 (which removes the vertical page limit), while keeping FitToPagesWide at 1 to ensure the content width stays within one page.
Complete Example: Comprehensive Page Layout Configuration
The following example combines all of the settings above, configuring margins, orientation, paper size, print area, and print title rows for a single worksheet:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
pageSetup = sheet.PageSetup
# Page margins
pageSetup.TopMargin = 2.5
pageSetup.BottomMargin = 2
pageSetup.LeftMargin = 1
pageSetup.RightMargin = 1
# Page orientation and paper size
pageSetup.Orientation = PageOrientationType.Landscape
pageSetup.PaperSize = PaperSizeType.PaperA4
# Print area
pageSetup.PrintArea = "A1:H50"
# Print title rows (repeat row 1 on every page)
pageSetup.PrintTitleRows = "$1:$1"
# Scale to fit page width
pageSetup.FitToPagesTall = 0
pageSetup.FitToPagesWide = 1
workbook.SaveToFile("FullPageSetup.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
This code is well-suited for a typical landscape A4 report scenario: it applies sensible margins, restricts the print range, ensures the header row repeats on every page, and scales the content to fit the page width automatically.
Summary
This article covered several common techniques for configuring Excel page layout with Python, including page margins, orientation, paper size, print area, print title rows/columns, and fit-to-page scaling. These settings are invaluable in everyday office workflows and automated report generation, allowing developers to finalize print parameters programmatically and reduce manual intervention.
Beyond the features discussed here, Spire.XLS also supports additional page setup properties such as print quality, first page number, and header/footer margins — all of which can be explored further based on your specific requirements.
Top comments (0)