简体   繁体   English

工作表中命名范围的字典

[英]Dictionary of named ranges in sheet

I have an Excel spreadsheet with a large number of named ranges in it. 我有一个Excel电子表格,其中包含大量命名范围。 I would like to be able to get data from them in an efficient manner. 我希望能够以有效的方式从他们那里获取数据。 Preferably in a python dictionary with the name of the range as the key and the value as the value. 优选地在python字典中,范围的名称作为键,值作为值。 Or someway to make a pseudo workbook that lets me read a sheet without all the calls to excel. 或者某种程度上制作一个伪工作簿,让我在没有所有excel调用的情况下阅读工作表。

Right now I am getting the value from each named range individually. 现在我分别从每个命名范围中获取值。 This works, but is very slow. 这有效,但速度很慢。 Ideally there would be one or two calls to get all of the named ranges. 理想情况下,将有一个或两个调用来获取所有命名范围。 I am currently using a @xw.sub to get the values. 我目前正在使用@ xw.sub来获取值。

@xw.sub
def get_named_ranges():
    wb = xw.Book.caller()
    ranges = {}
    ranges['MyNamedRange1'] = wb.sheets['sheet1'].['MyNamedRange1'].value
    ranges['MyNamedRange2'] = wb.sheets['sheet1'].['MyNamedRange2'].value
    ranges['MyNamedRange3'] = wb.sheets['sheet1'].['MyNamedRange3'].value

命名范围示例

You can perhaps use Pandas for this. 你也许可以使用Pandas

在此输入图像描述

from pandas import *
xls = ExcelFile('test.xlsx')
df = xls.parse(xls.sheet_names[0])
print(df.to_dict())

在此输入图像描述

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

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