简体   繁体   English

使用openpyxl冻结窗格的前两行和列

[英]Freeze Panes first two rows and column with openpyxl

Trying to freeze the first two rows and first column with openpyxl, however, whenever doing such Excel says that the file is corrupted and there is no freeze.尝试使用 openpyxl 冻结前两行和第一列,但是,无论何时执行此类 Excel 表示文件已损坏并且没有冻结。

Current code:当前代码:

workbook = openpyxl.load_workbook(path)
worksheet = workbook[first_sheet]
freeze_panes = Pane(xSplit=2000, ySplit=3000, state="frozen", activePane="bottomRight")
worksheet.sheet_view.pane = freeze_panes

Took a look at the documentation, however, there is little explanation on parametere setting.看了一下文档,但是,关于参数设置的解释很少。

Desired output:所需的 output: 在此处输入图像描述

Came across this answer, however, it fits a specific use case, hence, wanted to make a general question for future reference: How to split Excel screen with Openpyxl?遇到这个答案,但是,它适合特定的用例,因此,想提出一个一般性问题以供将来参考: 如何使用 Openpyxl 拆分 Excel 屏幕?

To freeze the first two rows and first column, use the sample code below... ws.freeze_panes works.要冻结前两行和第一列,请使用下面的示例代码... ws.freeze_panes有效。 Note that, like you would do in excel, select the cell above and left of which you want to freeze.请注意,就像您在 excel、select 中所做的那样,您要冻结其上方和左侧的单元格。 So, in your case, the cell should be B3 .因此,在您的情况下,单元格应该是B3 Hope this is what you are looking for.希望这是您正在寻找的。

import openpyxl
wb=openpyxl.load_workbook('Sample.xlsx')
ws=wb['Sheet1']
mycell = ws['B3']
ws.freeze_panes = mycell 
wb.save('Sample.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM