简体   繁体   English

使用Openpyxl在excel中找不到活动或选定的单元格

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

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. 我想使用python在excel电子表格当前活动表中查找当前活动或选定单元格的地址或坐标。

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. 但是,这是两年来第一次编写第一个VBA和现在编写的Python,我还无法仅仅搜索并找到答案。 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. 我已经阅读了readthedocs( http://openpyxl.readthedocs.org/en/latest/_modules/index.html )上的代码,并浏览了openpyxl.cell.cell,openpyxl.worksheet.worksheet,openpyxl.worksheet。查看代码。 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. 可能是因为openpyxl实际上正在查看已保存的电子表格,该电子表格可能不包含要选择的最后一个单元格上的任何数据。 I've tried it both in Python 3.4.3 and 2.7.11. 我在Python 3.4.3和2.7.11中都尝试过。 Using openpyxl 2.4.0. 使用openpyxl 2.4.0。 Here's the code that got me the closest to my goal. 这是使我最接近目标的代码。 I was running it in Python3. 我在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. 我很想编写一个VBA宏,然后从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. 任何帮助将不胜感激。

It's difficult to see the purpose of an active cell for a library like openpyxl as it is effectively a GUI artefact. 很难看到像openpyxl这样的库的活动单元格的用途,因为它实际上是GUI工件。 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. 但是,由于openpyxl努力实现OOXML规范,因此应该可以读取或写入前一个应用程序存储的值。

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

Consider the win32com library to replicate the Excel VBA property, ActiveCell . 考虑使用win32com库来复制Excel VBA属性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.). Openpyxl的此属性方法可能有限,而wind32com允许Python充分利用Windows程序的COM库,包括MS Office Suite(Excel,Word,Access等)。 You can even manipulate files as a child process as if your were directly writing VBA. 您甚至可以将文件作为子进程来处理,就像直接编写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

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

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