使用Openpyxl在excel中找不到活动或选定的单元格 [英] Can't find the active or selected cell in excel using Openpyxl

查看:498
本文介绍了使用Openpyxl在excel中找不到活动或选定的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python在excel电子表格当前活动表中查找当前活动或选定单元格的地址或坐标.

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.

到目前为止,我所能做的就是后者.也许我只是使用错误的单词进行搜索.但是,这是两年来第一次编写第一个VBA和现在编写的Python,我还无法仅仅搜索并找到答案.即使花了我半天的时间.
我在readthedocs( http://openpyxl.readthedocs.org /en/latest/_modules/index.html ) 并浏览了openpyxl.cell.cell,openpyxl.worksheet.worksheet,openpyxl.worksheet.views代码.最后一个似乎有一些希望,并导致我编写下面的代码.尽管如此,还是没有喜悦,我似乎无法用我的在线搜索来表达自己的观点,从而无法精确定位有关查找实际活动/选定单元格的结果.可能是因为openpyxl实际上正在查看已保存的电子表格,该电子表格可能不包含要选择的最后一个单元格上的任何数据. 我在Python 3.4.3和2.7.11中都尝试过.使用openpyxl 2.4.0. 这是使我最接近目标的代码.我在Python3中运行它.

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)

哪个给我以下?

['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)

我得到:

Values must be of type <class 'int'>

我认为这是因为这些仅用于编写而不用于查询.我很想编写一个VBA宏,然后从python运行它的想法.但是,此代码将与我无法控制的电子表格一起使用.由不一定有能力解决任何问题的人组成.我不认为我有能力写出足以应付任何可能出现的问题的好东西.

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.

推荐答案

很难看到像openpyxl这样的库的活动单元格的用途,因为它实际上是GUI工件.但是,由于openpyxl努力实现OOXML规范,因此应该可以读取或写入前一个应用程序存储的值.

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

这篇关于使用Openpyxl在excel中找不到活动或选定的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆