Python openpyxl 如何设置活动或选定的单元格

Mat*_*Cox 8 python python-3.x openpyxl

在带有 openpyxl 的 Python 中,我想在用户打开电子表格时更改活动单元格。

这会产生“A1”:

 print("Active Cell: " + WorkSheetOne.sheet_view.selection[0].activeCell)
Run Code Online (Sandbox Code Playgroud)

打开文件时会产生错误(文件损坏):

 WorkSheetOne.sheet_view.selection[0].activeCell = 'A4'
Run Code Online (Sandbox Code Playgroud)

How can I set the active/selected cell to something other than A1?

Mat*_*Cox 9

对于 openpyxls 2.3.2 版本,我可以使用以下方法:

 WorkSheetOne.sheet_view.selection[0].activeCell = 'A4'
 WorkSheetOne.sheet_view.selection[0].sqref = 'A4'
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助。

  • 这是针对不再受支持的旧版本库的。您应该链接到正确的答案。 (2认同)
  • 我尝试了这个答案,它似乎适用于当前版本(我有 3.0.3)。 (2认同)