如何用Openpyxl拆分Excel屏幕?

2024-05-12 18:38:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用Openpyxl垂直拆分Excel屏幕(在Excel中,功能区“视图”选项卡中的“拆分”按钮)。我还没找到指导,怎么做。但是我找到了这个网页(https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.views.html),我认为“ySplit”属性可能是解决方案。不幸的是,我还没能想出如何正确使用它。我尝试了以下代码:

wb = openpyxl.load_workbook('file.xlsx')
sh = wb.active
sh.sheet_view.pane.ySplit = 20

EDIT: But this code does not work: AttributeError: 'NoneType' object has no attribute 'ySplit'.

我还尝试了上面代码的一些变体(使用ySplit)。但是没有成功。如果有人能帮我找到一个方法,如何分割屏幕,我将不胜感激。在

提前谢谢。在

EDIT2: The solution was provided by stovfl in comments. The code should be:

^{pr2}$

Tags: the代码视图屏幕shcodeexcel按钮
1条回答
网友
1楼 · 发布于 2024-05-12 18:38:26

Question How to split Excel screen with Openpyxl?

要定义,要显示拆分的工作表,必须创建一个openpyxl.worksheet.views.Pane对象并将其分配给myWorksheet.sheet_view.pane。在

from openpyxl.worksheet.views import Pane

wb = openpyxl.load_workbook('file.xlsx')
ws = wb.active

ws.sheet_view.pane = Pane(xSplit=20.0, ySplit=None, 
                          topLeftCell='C1', activePane='topLeft', state='split')

wb.save('file.xlsx')

openPyXL - worksheet.views.Pane

class openpyxl.worksheet.views.Pane(xSplit=None, ySplit=None, 
                                    topLeftCell=None, 
                                    activePane='topLeft', state='split')[source]

activePane
Value must be one of {‘topLeft’, ‘bottomRight’, ‘topRight’, ‘bottomLeft’}

state
Value must be one of {‘split’, ‘frozen’, ‘frozenSplit’}

topLeftCell
Values must be of type <class ‘str’>

xSplit
Values must be of type <class ‘float’>

ySplit
Values must be of type <class ‘float’>

相关问题 更多 >