python xlwings:从panda框架中填充用户表单的组合框

ewm0tg9j  于 2023-10-14  发布在  Python
关注(0)|答案(1)|浏览(108)

我有一个包含SQL查询记录的pandas框架。我想使用xlwings将此框架的数据插入到用户表单的组合框中。
我有这样的东西:

cb = xw.Book.caller().sheets.active.shapes("ufm_PortfolioCompo.cbb_DALI_ptf_id").api.ControlFormat
cb.RemoveAllItems()
for id in df_dali_ptf_id:
    cb.AddItem(id)
    cb.ListIndex = 1

这是我得到的错误消息:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\xlwings\_xlwindows.py", line 1134, in __call__
    return self._wrap(xl=self.xl.Item(key))
  File "C:\ProgramData\Anaconda3\lib\site-packages\xlwings\_xlwindows.py", line 63, in __call__
    v = self.__method(*args, **kwargs)
  File "<COMObject <unknown>>", line 2, in Item
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, "The item with the specified name wasn't found.", None, 0, -2147024809), None)

如有任何帮助,我们将不胜感激!非常感谢提前!
我看了xlwings的文档。
我也试着模仿一个来自这篇文章的例子;
xlwings-how-to-write-values-to-a-combo-box-from-python-udf

ffvjumwh

ffvjumwh1#

你可以把数据框写在工作表上,然后用你的组合框引用它;

import xlwings as xw
import pandas as pd

data = pd.DataFrame({
    "A": ["foo1", "foo2", "foo3", "foo4", "foo5"],
})

with xw.App(visible=False) as app:
    wb = xw.Book()
    ws = wb.sheets[0]

    ### Write the Dataframe to the sheet
    ws["A1"].options(index=False, header=False, expand="table").value = data

    ### Create Combobox
    cb = ws.api.Shapes.AddFormControl(2, 96, 0.6, 48, 16.2)
    ### Reference the dataframe data
    cb.OLEFormat.Object.ListFillRange = "$A$1:$A$5"

    wb.save('foo_out.xlsx')

页:-

相关问题