首页 > 解决方案 > 如何使用 Cython Windows DLL(比如 square.pyd)作为 MS Excel 的对象引用?

问题描述

使用 Cython 从 square.pyx 和 setup.py 文件生成一个 square.pyd 文件以及相应的 square.lib、square.exp、square.cpp。如何将此 square.pyd 文件与 Excel VBA 一起使用?

'''square.pyx
# -*- coding: utf-8 -*-
# distutils: language=c++

def square( unsigned int x):
    return (x * x)
'''

'''setup.py
# -*- coding: utf-8 -*-
from setuptools import setup
from Cython.Build import cythonize

setup(
      ext_modules = cythonize("square.pyx", annotate=True))
'''

'''build square.pyd
c:\myDir> python setup.py build_ext --inplace
'''

如何在 MS EXCEL 中使用 square.pyd (Windows DLL) 作为对象参考?

标签: pythonexcelvbacython

解决方案


我不完全确定您的意思,但希望这可能对您有所帮助。当您想在 MS Excel 中添加额外的库时,您可以在 VBA 编辑器 (ALT+F11) 中执行此操作。然后你找到你想工作的项目(工作簿),顶部有“工具”菜单。在那里您选择“参考”并浏览并选择您的 DLL。然后你应该能够在 VBA 代码中调用提到的库中的函数。


推荐阅读