xlwings: Write Excel macro using python instead of VBA
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了xlwings: Write Excel macro using python instead of VBA,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2414字,纯文字阅读大概需要4分钟。
内容图文
![xlwings: Write Excel macro using python instead of VBA](/upload/InfoBanner/zyjiaocheng/1268/0582c3bae8a94713a07de809a7600a63.jpg)
i want to write Excel macros to deal with the data, but i am not familiar with VBA language. so i decide to use python instead of VBA.
at the beginning, i find xlrd,xlwt for python Excel operations, but that doesn‘t support Excel macro. Finally i find xlwings.
xlwings - Make Excel Fly!
- Scripting: Automate/interact with Excel from Python using a syntax close to VBA.
- Macros: Replace VBA macros with clean and powerful Python code.
- UDFs: Write User Defined Functions (UDFs) in Python (Windows only)
You can find more details in the offical document links: https://www.xlwings.org/
xlwings have many power functions, i will only introduce how to write Excel macros
1. Installation
The easiest way to install xlwings is via pip:
pip install xlwings
or conda:
conda install xlwings
i suggest that you install Anaconda, it already includes a lot of useful moudles, and xlwings will depend on part of them.
2. Add-in
you need Windows command line to install/remove the add-in in Excel.
xlwings addin install
: Copies the xlwings add-in to the XLSTART folder
After installing the add-in, it will be available as xlwings tab on the Excel Ribbon. you need to give the interpreter path.
Interpreter
: This is the path to the Python interpreter (works also with virtual or conda envs), e.g. "C:\Python35\pythonw.exe"
or "/usr/local/bin/python3.5"
. An empty field defaults to pythonw
that expects the interpreter to be set in the PATH
on Windows or .bash_profile
on Mac.
3.Quickstart
you need Windows command line to create necessary files automatically.
-
xlwings quickstart myproject
This command is by far the fastest way to get off the ground: It creates a new folder myproject
with an Excel workbook that already has the reference to the xlwings addin and a Python file, ready to be used right away:
myproject |--myproject.xlsm |--myproject.py
4.VBA: RunPython
In the VBA Editor (Alt-F11
), write the code below into a VBA module. you can add new module via Insert > Module
Sub HelloWorld() RunPython ( " import hello; hello.world() " ) End Sub
This calls the following code in hello.py
:
# hello.py import numpy as np import xlwings as xw def world(): wb = xw.Book.caller() wb.sheets[0].range(‘A1‘).value = ‘Hello World!‘
You can then attach HelloWorld
to a button or run it directly in the VBA Editor by hitting F5
.
原文:http://www.cnblogs.com/frost-hit/p/7286298.html
内容总结
以上是互联网集市为您收集整理的xlwings: Write Excel macro using python instead of VBA全部内容,希望文章能够帮你解决xlwings: Write Excel macro using python instead of VBA所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。