简体   繁体   English

在Python中使用XLwings从下拉列表中进行选择

[英]Pick From Drop-Down List Using XLwings in Python

I have an Excel sheet with a few cells locked with data validation using a drop-down list. 我有一个Excel工作表,其中有几个单元格使用下拉列表锁定了数据验证。 I want to give a value to those cells using XLwings. 我想给那些使用XLwings的单元格一个值。 I have seen some similar questions, but nothing solves my issue. 我见过类似的问题,但没有任何解决方法。

I tried with: 我尝试过:

app = xw.App(visible=True)
wb = app.books.open(copy_file)
sht = wb.sheets['Sheet1']
list = sht.range('C21').api.Validation.Formula1[1:]

Honestly I don't know how that last line is supposed to work, I found it at https://github.com/ZoomerAnalytics/xlwings/issues/901 . 老实说,我不知道最后一行应该如何工作,我在https://github.com/ZoomerAnalytics/xlwings/issues/901上找到了它。 When I try to run it, it just throws an error in xlwindows.py and stops the code. 当我尝试运行它时,它只会在xlwindows.py中引发错误并停止代码。 Anyone can help? 有人可以帮忙吗?

I can't be sure, but after looking at the link you posted, I think the problem is that you're trying to set a range (the [1:] part means from index 1 to the end, not a single value). 我不确定,但是在查看您发布的链接后,我认为问题在于您正在尝试设置范围([1:]部分表示从索引1到末尾,而不是单个值) 。 In the example from the link, it is asking for those values to be returned , so that was OK. 在链接的示例中,它要求返回这些值,这样就可以了。 But you're trying to set the value, and you can't set a drop-down menu to an entire range. 但是您正在尝试设置该值,并且无法将下拉菜单设置为整个范围。 Delete the ':' and you might have more success. 删除“:”,您可能会获得更大的成功。

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

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