Python openpyxl How to SET the active or selected cell
Question:
In Python with openpyxl I’d like to change the active cell when the user opens the spreadsheet.
This produces ‘A1’:
print("Active Cell: " + WorkSheetOne.sheet_view.selection[0].activeCell)
This produces an error when the file is opened (Corrupted file):
WorkSheetOne.sheet_view.selection[0].activeCell = 'A4'
How can I set the active/selected cell to something other than A1?
Answers:
For openpyxls version 2.3.2 I got this to work:
WorkSheetOne.sheet_view.selection[0].activeCell = 'A4'
WorkSheetOne.sheet_view.selection[0].sqref = 'A4'
Hope this helps someone.
If you got openpyxl version 2.5.4 or later, try typing below to set active cell
ws.cell(row=4, column=1)
wb.save("spreadsheet.xlsx")
assume ws is the worksheet you use, and you want to set cell A4 active. Make sure you save the spreadsheet in your code.
I’m using openpyxl version 3.0.7 and found that for an excel file I have it was necessary to also do the same definition for selection[1].
worksheet.views.sheetView[0].selection[0].activeCell = 'A1'
worksheet.views.sheetView[0].selection[0].sqref = 'A1'
worksheet.views.sheetView[0].selection[1].activeCell = 'A1'
worksheet.views.sheetView[0].selection[1].sqref = 'A1'
or
data_sheet.sheet_view.selection[0].activeCell = 'A1'
data_sheet.sheet_view.selection[0].sqref = 'A1'
data_sheet.sheet_view.selection[1].activeCell = 'A1'
data_sheet.sheet_view.selection[1].sqref = 'A1'
I hope this helps someone.
Based on the accepted answer and the answer by BryanSJT, I created this loop which should set all selections correctly to A1
for selection in ws.views.sheetView[0].selection:
selection.activeCell = "A1"
selection.sqref = "A1"
In Python with openpyxl I’d like to change the active cell when the user opens the spreadsheet.
This produces ‘A1’:
print("Active Cell: " + WorkSheetOne.sheet_view.selection[0].activeCell)
This produces an error when the file is opened (Corrupted file):
WorkSheetOne.sheet_view.selection[0].activeCell = 'A4'
How can I set the active/selected cell to something other than A1?
For openpyxls version 2.3.2 I got this to work:
WorkSheetOne.sheet_view.selection[0].activeCell = 'A4'
WorkSheetOne.sheet_view.selection[0].sqref = 'A4'
Hope this helps someone.
If you got openpyxl version 2.5.4 or later, try typing below to set active cell
ws.cell(row=4, column=1)
wb.save("spreadsheet.xlsx")
assume ws is the worksheet you use, and you want to set cell A4 active. Make sure you save the spreadsheet in your code.
I’m using openpyxl version 3.0.7 and found that for an excel file I have it was necessary to also do the same definition for selection[1].
worksheet.views.sheetView[0].selection[0].activeCell = 'A1'
worksheet.views.sheetView[0].selection[0].sqref = 'A1'
worksheet.views.sheetView[0].selection[1].activeCell = 'A1'
worksheet.views.sheetView[0].selection[1].sqref = 'A1'
or
data_sheet.sheet_view.selection[0].activeCell = 'A1'
data_sheet.sheet_view.selection[0].sqref = 'A1'
data_sheet.sheet_view.selection[1].activeCell = 'A1'
data_sheet.sheet_view.selection[1].sqref = 'A1'
I hope this helps someone.
Based on the accepted answer and the answer by BryanSJT, I created this loop which should set all selections correctly to A1
for selection in ws.views.sheetView[0].selection:
selection.activeCell = "A1"
selection.sqref = "A1"