如何在XLWINGS中使用输入箱来提示用户范围内的范围并在Python中进行交互式(或替代方案)检索单元格?

发布于 2025-01-26 01:46:29 字数 1258 浏览 2 评论 0原文

在执行Python笔记本电脑期间,我想提示用户在Excel Workbook中的范围,以便以交互方式检索单元格(用于在Pandas中进行进一步详细说明,我用来克服Excel中的限制)。在VBA中实现这一目标的用户友好型(点击)示例如下:

Dim LoopR As Range: Set LoopR = Application.InputBox("Select the range (rows) to iterate over:", "Iteration Range Rows Input", Type:=8)

它创建一个对话框,用户可以手动选择范围并将其返回。

我试图在XLWINGS中实现相同的操作,但以下内容:

import xlwings as xw
app = xw.App(add_book=False)
wb = app.books.open('path/to/file.xlsx')
rng = app.api.InputBox("Select the range (rows) to iterate over:", "Iteration Range Rows Input", Type=8)

它按预期在Excel应用程序中创建对话框;但是,rng仅存储所选范围左上角单元的内容(作为字符串),在我的情况下,这会破坏目的。我没有找到有关我的特定问题的任何线程,也不知道如何使用Win23API模块来实现相同的线程。

我认为可以在XLWINGS中实现我想要的目标,而不必通过键入或进行硬编码来尴尬地输入范围。请注意,我无法升级XLWING(我的版本是0.16.0),因此无法使用方便 xw.load() 函数。

我欢迎解决方案,解释或有效的替代方案。

PS:我也提出了这个问题提供以下系统规格:

  1. OS Windows Server 2019(1809)
  2. XLWINGS 0.16.0的版本,Office 365 MSO(2112)和Python 3.6.9

During the execution of the Python notebook I want to prompt the user for a range in the Excel workbook in order to retrieve the cell contents interactively (for further elaboration in pandas, which I use to overcome limitations in Excel). An user-friendly (point-and-click) example to achieve that in VBA would be as follows:

Dim LoopR As Range: Set LoopR = Application.InputBox("Select the range (rows) to iterate over:", "Iteration Range Rows Input", Type:=8)

Which creates a dialog box where the user can select the range manually, and returns it.

I attempted to achieve the same in xlwings with the following:

import xlwings as xw
app = xw.App(add_book=False)
wb = app.books.open('path/to/file.xlsx')
rng = app.api.InputBox("Select the range (rows) to iterate over:", "Iteration Range Rows Input", Type=8)

Which creates the dialog box in the Excel application as expected; however, rng only stores the content (as string) of the top-left cell of the selected range, which in my case defeats the purpose. I haven't found any threads about my specific issue and wouldn't know how to achieve the same using the win23api module.

I think it's possible to achieve what I want in xlwings without having to awkwardly input the range by typing or hardcoding it. Please note that I can't upgrade xlwings (my version is 0.16.0) and therefore can't use the convenient xw.load() function.

I welcome solutions, explanations, or valid alternatives.

P.S.: I've also raised this issue in the xlwings Github repository, where additionally I provided the following system specifications:

  1. OS Windows Server 2019 (1809)
  2. Versions of xlwings 0.16.0, Excel for Office 365 MSO (2112), and Python 3.6.9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文