使用Pyxll将数组从Excel导入到Python Pandas中

我拼命地尝试使用Pyxll为了写一些Excel函数,获取一堆数组,加载它们在Python中,将它们转换为大pandas数据框,播放数据一点,然后返回最终的DataFrame。 现在,为了返回DataFrame,我发现了pyxll的例子 ,但不pipe我怎么尝试,我似乎无法将我加载的excel数组转换成我可以使用的pandas DataFrame。

例如,我尝试使用下面的代码,但没有运气。 也许如果我有一些方法知道Python中加载了什么以及它的外观,也许我有更好的机会来理解如何操作数据,但我不知道如何查看Canopy输出区域上的输出。

有谁知道一个简单的方法来从Excel导入数据到Python,处理它,然后将其返回到Excel,而不必保存文件,加载到Python,处理数据和覆盖现有的文件?

@xl_func("string[] name, var[] day, string[] method, string[] currency, numpy_array amounts, date[] dates: dataframe") def test(name, day, method, currency, amounts, dates): df_name = DataFrame(name, columns = ['Name']) df_method = DataFrame(method, columns = ['Method']).ix[1:] df_currency = DataFrame(currency, columns = ['Currency']).ix[1:] df = df_name.join(df_method).join(df_currency) cols = ['Name', 'Currency', 'Method'] df = df[cols] return DataFrame(dates) 

看看(我的)图书馆xlwings 。 它使得发送DataFrame来回一样简单:

 >>> from xlwings import Workbook, Range >>> import pandas as pd >>> wb = Workbook() # Pass in the path of a file to work with an existing Workbook >>> df = pd.DataFrame([[1., 2.], [3., 4.]], columns=['one', 'two']) >>> Range('A1', index=False).value = df # send it over to Excel >>> data = Range('A1').table.value # read it back >>> pd.DataFrame(data[1:], columns=data[0]) one two 0 1 2 1 3 4 

尤其请参阅有关DataFrames的文档以及如何从VBA调用它。

PyXLL可以使用自定义types接受和返回pandas数据框。

看看这个例子: https : //github.com/pyxll/pyxll-examples/blob/master/pandas

查看日志文件的输出结果 要使用IPython提示符在Python中以交互方式进行交互,请使用以下示例: https : //github.com/pyxll/pyxll-examples/tree/master/ipython

更好地坚持function,而不是诉诸命名的范围。

您也可以使用PyXLL来注册键盘快捷键。 看到这个例子的自动调整数组公式输出的快捷方式: https : //github.com/pyxll/pyxll-examples/blob/master/shortcuts/resize_array_formula.py

如果您需要更多帮助,请联系支持以获得快速响应。

查看用于xlrd (Python 2和3), xlwt (仅Python 2)和xlsxwriter (Python 2和3)模块。 pandas在其代码中已经挂钩了它们; 你可以在这里阅读关于read_excelto_excel函数的所有信息。