Charles L.
Charles L.

Reputation: 6275

Set workbook view with openpyxl?

Is it possible to set the Workkbook View to "Page Layout" with openpyxl? Looking on stackoverflow and the openpyxl docs I can't seem to find it. Is it possible?

enter image description here

Upvotes: 2

Views: 1263

Answers (1)

Jakub Szlaur
Jakub Szlaur

Reputation: 2132

Yes, it is possible with this code:

from openpyxl import Workbook

wb = Workbook()
ws = wb.active

#Value must be one of {'pageBreakPreview', 'pageLayout', 'normal'}
ws.sheet_view.view = "pageLayout" 

How did I find out?

To my surprise I also could not find any tutorials or anything in the docs on this topic. I did a little digging and if you type into the terminal:

print(ws.sheetview)

A series of parameters will pop out, including the one we are looking for (the view attribute):

Parameters:
windowProtection=None, showFormulas=None, showGridLines=None, showRowColHeaders=None, showZeros=None, rightToLeft=None, tabSelected=None, showRuler=None, showOutlineSymbols=None, defaultGridColor=None, showWhiteSpace=None, veSymbols=None, defaultGridColor=None, showWhiteSpace=None, view=None, topLeftCell=None, colorId=None, zoomScale=None, zoomScaleNormal=None, zoomScaleSheetLayoutViewalePageLayoutNone,ne, selection=[<openpyxl.worksheet.views.S=None, zoomScalePageLayoutView=None, zoomToFit=None, workbookViewId=0, pane=None, selection=[<openpyxl.worksheet.views.Selection object>

You can then set this attribute according to the code on the top of the answer to the three predetermined values otherwise you will get an ValueError: Value must be one of {'pageBreakPreview', 'pageLayout', 'normal'}.

Upvotes: 3

Related Questions