简体   繁体   English

在 xlwings 中创建和命名表

[英]Creating and naming a table in xlwings

I am trying to use xlwings to interface Excel from some of my Python code and unfortunately only found stackoverflow threads or documentation on accessing/reformatting already existing Excel tables rather than creating new ones from scratch.我正在尝试使用xlwings从我的一些 Python 代码中连接 Excel,但不幸的是,我只找到了有关访问/重新格式化现有Excel 表格的 stackoverflow 线程或文档,而不是从头开始创建新表格。

edit: solution added based on David Zemen's answer (additional getters and setters for ListObject properties are available here .编辑:根据 David Zemen 的回答添加的解决方案( 此处提供了 ListObject 属性的其他 getter 和 setter。

import xlwings

df = ... # initialising some pandas DataFrame

book = xlwings.Book()
ws = book.sheets.active
ws.range("A1").options(index=False).value = df

# TODO: Create table
# downside: does not seem to be possible to define multiple tables
#           within a Sheet (based on explicit ranges)
tbl = ea_sheet.api.ListObjects.Add(1) 


# TODO: Set the name/formatting of the table
tbl.DisplayName = "tabname"  # sets the tablename

I know that I can expand the range selection using ws.range("A1").expand('table') and know that I can use the .name property of ranges to define them as named range.我知道我可以使用ws.range("A1").expand('table')扩展范围选择,并且知道我可以使用范围的.name属性将它们定义为命名范围。

However, does xlwings provide any means to create a table from a range (similar to using Home - Format as Table in the Excel UI)?但是,xlwings 是否提供了从某个范围创建表格的任何方法(类似于在 Excel UI 中使用 Home - Format as Table)?

I'm not sure if xlwings directly supports the creation of ListObjects, but since it exposes the api , you could try something like:我不确定 xlwings 是否直接支持 ListObjects 的创建,但由于它公开了api ,您可以尝试以下操作:

xlSrcRange = 1
xlYes = 1
ws.range("A1").options(index=False).value = df
tbl_range = ws.range("A1").expand('table')
ws.api.ListObjects.Add(xlSrcRange, tbl_range, xlListObjectHasHeaders=xlYes)

Unfortunately, this does not seem to work and fails silently.不幸的是,这似乎不起作用并且默默地失败了。

import xlwings as xw
import pandas as pd
import numpy as np
# creating a dummy dataframe
data = np.array(np.random.randint(0,100,(3,4)))
columns = ['A','B','A','C']
index = ['Row 1', 'Row 2', 'Row 3']
frame = pd.DataFrame(data, index=index, columns=columns)
book = xw.Book()
ws = book.sheets.active
ws.range("A1").options(index=False).value = frame
tbl_range = ws.range("A1").expand('table')
ws.api.ListObjects.Add(1, tbl_range)  ## This line fails, but no error is raised. 

I suspect there is some problem passing the xlwings Range as the second parameter to the Add method.我怀疑将 xlwings 范围作为第二个参数传递给Add方法时存在一些问题。

I tried a few other things and if we omit the range argument, the ws.api call seems to work when I run it (with the caveat that the current ActiveCell/selection must be within the table area/used range):我尝试了其他一些事情,如果我们省略 range 参数, ws.api调用在我运行时似乎可以工作(但需要注意的是,当前的 ActiveCell/选择必须在表区域/使用范围内):

ws.api.ListObjects.Add(1)

在此处输入图片说明

Finally, I was able to get it to work with the ListObjects.Add method but using the .api to pass a valid Excel range rather than an xlwings range:最后,我能够让它与ListObjects.Add方法一起使用,但使用.api传递有效的 Excel 范围而不是 xlwings 范围:

ws.api.ListObjects.Add(1, ws.api.Range(tbl_range.address))

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

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