Setting Margins

How to Set Margins

Aspose.Cells for Python via .NET provides a class, Workbook, that represents an Excel file. The Workbook class contains the worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class.

The Worksheet class provides the page_setup property used to set the page setup options for a worksheet. The page_setup attribute is an object of the PageSetup class that enables developers to set different page layout options for a printed worksheet. The PageSetup class provides various properties and methods used to set page setup options.

How to Set Page Margins

Set page margins (left, right, top, bottom) using PageSetup class members. A few of the methods are listed below which are used to specify page margins:

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create a workbook object
workbook = Workbook()
# Get the worksheets in the workbook
worksheets = workbook.worksheets
# Get the first (default) worksheet
worksheet = worksheets[0]
# Get the pagesetup object
pageSetup = worksheet.page_setup
# Set bottom,left,right and top page margins
pageSetup.bottom_margin = 2.0
pageSetup.left_margin = 1.0
pageSetup.right_margin = 1.0
pageSetup.top_margin = 3.0
# Save the Workbook.
workbook.save(dataDir + "SetMargins_out.xls")

How to Center on Page

It is possible to center something on a page horizontally and vertically. For this, there are some useful members of the PageSetup class, center_horizontally and center_vertically.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create a workbook object
workbook = Workbook()
# Get the worksheets in the workbook
worksheets = workbook.worksheets
# Get the first (default) worksheet
worksheet = worksheets[0]
# Get the pagesetup object
pageSetup = worksheet.page_setup
# Specify Center on page Horizontally and Vertically
pageSetup.center_horizontally = True
pageSetup.center_vertically = True
# Save the Workbook.
workbook.save(dataDir + "CenterOnPage_out.xls")

Set header and footer margins with the PageSetup class members such as header_margin and footer_margin.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create a workbook object
workbook = Workbook()
# Get the worksheets in the workbook
worksheets = workbook.worksheets
# Get the first (default) worksheet
worksheet = worksheets[0]
# Get the pagesetup object
pageSetup = worksheet.page_setup
# Specify Header / Footer margins
pageSetup.header_margin = 2.0
pageSetup.footer_margin = 2.0
# Save the Workbook.
workbook.save(dataDir + "CenterOnPage_out.xls")