Can't find the active or selected cell in excel using Openpyxl

别来无恙 提交于 2019-12-25 09:49:08

问题


I want to use python to find what the address or coordinates of the currently active or selected cell in an excel spreadsheets currently active sheet.

So far all I've been able to do is the latter. Perhaps I'm just using the wrong words to search. However, this is the first time in two years of writing first VBA and now Python that I haven't been able to just search and find the answer. Even if it took me half a day.
I've crawled through the code at readthedocs (http://openpyxl.readthedocs.org/en/latest/_modules/index.html) and looked through the openpyxl.cell.cell, openpyxl.worksheet.worksheet, openpyxl.worksheet.views code. The last seemed to have some promise and led me to writing the code below. Still, no joy, and I don't seem to be able to phrase my online searches to be able to pinpoint results that talk about finding the actual active/selected cell. Perhaps this is because openpyxl is really looking at the saved spreadsheet which might not include any data on the last cell to be selected. I've tried it both in Python 3.4.3 and 2.7.11. Using openpyxl 2.4.0. Here's the code that got me the closest to my goal. I was running it in Python3.

from openpyxl.worksheet.views import Selection
import openpyxl


wb = openpyxl.load_workbook('example.xlsx')
ws = wb.active
print(wb.get_sheet_names())
print(ws)
print(Selection.activeCell)

Which gives me the below.

['Sheet1', 'Sheet2', 'Sheet3']
<Worksheet "Sheet3">
Values must be of type <class 'str'>

I put in the first two prints just to prove to myself that I'm actually accessing the workbook/sheet.

If I change the last line to:

print(Selection.activeCellId)

I get:

Values must be of type <class 'int'>

I assume this is because these are only for writing not querying. I've toyed with the idea of writing a VBA macro and just running it from python. However, this code will be used with spreadsheets I don't control. By people who aren't necessarily capable of fixing any problems. I don't think I'm capable of writing something good enough to handle any problems that might crop up either.

Any help will be greatly appreciated.


回答1:


It's difficult to see the purpose of an active cell for a library like openpyxl as it is effectively a GUI artefact. Nevertheless, because openpyxl works hard to implement the OOXML specification it should be possible to read the value stored by the previous application, or write it.

ws.views.sheetView[0].selection[0].activeCell




回答2:


Consider the win32com library to replicate the Excel VBA property, ActiveCell. Openpyxl might have a limited method for this property while wind32com allows Python to fully utilize the COM libraries of Windows programs including the MS Office Suite (Excel, Word, Access, etc.). You can even manipulate files as a child process as if your were directly writing VBA.

import win32com.client

# OPEN EXCEL APP AND SPREADSHEET
xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Workbooks.Open('example.xlsx')

xlApp.ActiveWorkbook.Worksheets('Sheet1').Activate
print(xlApp.ActiveCell)

xlApp.ActiveWorkbook.Close(False)
xlApp.Quit

xlApp = None


来源:https://stackoverflow.com/questions/36751017/cant-find-the-active-or-selected-cell-in-excel-using-openpyxl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!