How to access the pages in a Worksheet to apply the openpyxl page module

  excel, openpyxl, python

I am trying to format the page margins of a template file that I load in via openpyxl. I’m trying to use from but I get the error AttributeError: ‘Worksheet’ object has no attribute ‘PageMargins’.

A sample of the code is below:

from openpyxl import load_workbook
from import PageMargins
wb = load_workbook('example.xlsx')
ws_example = wb['Sheet1']
ws_example.PageMargins(left=0.5, right=0.4, top=0.612, bottom=0.1, header=0.2, footer=0.3)

It makes sense that applying the page module function to an entire worksheet would get an error but I don’t understand how to access the seperate pages. The ‘Sheet1’ in this case is a 2 page sheet with some headers/footers that is planned to be printed out.

I tried for page in ws_example to apply PageMargins but each page is tuple. I have 3.0.9 openpyxl.

Source: Python Questions