如何访问工作表中的页面以应用 openpyxl 页面模块

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

我正在尝试格式化通过 openpyxl 加载的模板文件的页边距。我正在尝试使用 from openpyxl.worksheet.page.PageMargins 但我收到错误 AttributeError: 'Worksheet' object has no attribute 'PageMargins'.

代码示例如下:

from openpyxl import load_workbook
from openpyxl.worksheet.page 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)

将页面模块功能应用于整个作品sheet会出错是有道理的,但我不明白如何访问单独的页面。本例中的 'Sheet1' 是 2 页 sheet,其中一些 headers/footers 计划打印出来。

我尝试为 ws_example 中的页面应用 PageMargins,但每个页面都是元组。我有 3.0.9 openpyxl.

我认为您需要将返回的 PageMargins 对象分配给 sheet

page_margins 属性
ws_example.page_margins = PageMargins(left=0.5, right=0.4, top=0.612, bottom=0.1, header=0.2, footer=0.3)