2016-04-28 85 views

回答

0

您可以使用方括号来访问列,建议here

import xlwings as xw 
wb = xw.Workbook.active() 
xw.Range('TableName[ColumnName]').value 
+0

你是什么意思'TableName',因为我只有一张Excel表格,第一行填充了列名。 – Amani

1

您可以使用Pandas作为转换器xlwings 0.7.0的。对于这样的示例工作簿:

A B C 
1 4 7 
2 5 8 
3 6 9 

此代码将读取表中并允许您通过列标题访问数据。关键是.options(pd.DataFrame, index=False)位。该特定的调用将返回一个带有默认索引的Pandas DataFrame。

更多关于xlwings转换器的信息here

import xlwings as xw 
import pandas as pd 

def calc():  
    # Create a reference to the calling Excel xw.Workbook 
    wb = xw.Workbook.caller() 

    table = xw.Range('A1').table.options(pd.DataFrame, index=False).value 

    # Access columns as attributes of the Pandas DataFrame 
    print table.A 
    print table.B 

    # Access columns as column labels of the Pandas DataFrame 
    print table['A'] 
    print table['B'] 

if __name__ == '__main__': 
    path = "test.xlsm"   
    xw.Workbook.set_mock_caller(path) 
    calc()