0

我正在尝试使用 xlwings 从几个 xls 文件中进行简单的合并。为此,我有一个 all_files.xlsm 文件,其中包含一个分配有宏的按钮。宏看起来像:

Sub check_data()
    RunPython ("import python_code; python_code.consolidation()")
End Sub

在同一个文件夹中,我有一个文件 python_code.py,里面有函数“consolidation”。我还使用 Workbook.set_mock_caller() 以便有机会通过 python 接口运行代码。看起来像:

def consolidation(file_path):
    *** smth to get the data I need ***
    ...
    *** after I got data ***
    Range('A1').table.clear_contents() #string1
    Range('A1').value = data #string2

def main():
    consolidation(file_path)

if __name__ == '__main__':
    xl.Workbook.set_mock_caller(path_to_file)
    main()

问题是,当我通过 excel 文件中的按钮运行脚本时,最后两个字符串(string1 和 string2)- Range('A1').table.clear_contents() 和 Range('A1').value = data doesn'不工作。尽管其余代码工作正常(但是,它也包含 xlwing)。此外,如果我使用 set mock caller 通过 python 接口运行脚本,它工作得很好,包括 string1 和 string2(在代码中标记)。

非常感谢任何帮助和建议!

4

1 回答 1

1

我为我的问题找到了决定。要在 python 中使用 xlwings,您应该在您的 excel 文件中导入 xlwings.bas(请参阅 xlwings 手册)。事实证明,我在更新 xlwings 之前为这个宏导入了它。所以我删除了这个文件并导入了一个新文件。现在一切正常。

于 2015-12-12T23:57:06.307 回答