xlwings: Write Excel macro using python instead of VBA
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 pythonwthat 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 myprojectwith 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.



xlwings: Write Excel macro using python instead of VBA的更多相关文章
- 利用 Python 插件 xlwings 读写 Excel
Python 通过 xlwings 读取 Excel 数据 去年底公司让我做设备管理,多次委婉拒绝,最终还是做了.其实我比较喜欢技术.做管理后发现现场没有停机率统计,而原始数据有,每次要自己在Exce ...
- 一个使用xlwings操作excel数据优化60倍处理效率的案例
☞ ░ 前往老猿Python博文目录 ░ 一.引言 老猿在将自己的博文数据(包括url地址.标题和阅读数量)从博客中获取后,使用xlwings保存到excel对象时发现,不同的处理方法性能相差非常大. ...
- Working with Excel Files in Python
Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...
- 像Excel一样使用python进行数据分析
Excel是数据分析中最常用的工具,本篇文章通过python与excel的功能对比介绍如何使用python通过函数式编程完成excel中的数据处理及分析工作.在Python中pandas库用于数据处理 ...
- python excel 像 Excel 一样使用 python 进行数据分析
https://www.jb51.net/article/63216.htm 像 Excel 一样使用 python 进行数据分析 : https://www.cnblogs.com/nxld/p/ ...
- [转]Introduction - Run Excel Macro using VBScript
本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...
- 用python批量执行VBA代码
先说下背景环境 1. 公司需要问卷调查,有两份问卷, 1)是spss问卷,2)是excel问卷.spss问卷数据不全,但有各种标签, excel呢, 生成的数据直接把选项变成了值 2. 现在需要把ex ...
- 个人永久性免费-Excel催化剂功能第47波-VBA开发者喜爱的加密函数类
VBA的确是个很不错的编程工具,寄生在OFFICE内,无需安装庞大的开发环境,即开即用,方便灵活,能实现的事情也很多,但毕竟VBA是微软停止更新维护将近20年的一种语言,计算机的世界发展速度有多快大家 ...
- Python 使用 xlwings 往 excel 中写入一行数据的两种方法
该方法跟上一篇写入一列的方法相反,代码如下: # -*- coding:utf-8 -*- import xlwings as xw list1 = [1,2,3,4,5] list2 = [[1], ...
随机推荐
- OPPO R11s在哪里打开Usb调试模式的简单方法
就在我们使用pc链接安卓手机的时候,如果手机没有开启usb调试模式,pc则不能够成功读到我们的手机,如果遇到此情况我们需要找方法将手机的usb调试模式打开,本文我们记录一下OPPO R11s如何开启u ...
- R 升级版本
从3.4.1 升级到最新 懒得去官网下载 在R studio 执行一下代码 install.packages("installr") library(installr) updat ...
- java centos7配置查看jdk环境变量
[root@bogon java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64]# which java/usr/bin/java[root@bogon jav ...
- Java 数组算法列题解析
1.声明一个char类型的数组, 从键盘录入6个字符[1]遍历输出[2]排序[3]把char数组转化成一个逆序的数组 总结:分析问题时,先问自己,需要用到什么? 对于这题,第一步:先写一个main方 ...
- maxscript批量设置摄像机并保存渲染图
代码可直接运行 fn renderr = ( a=box() --新建一个立方体 ambientcolor = (color ) --设置环境光(搞不太懂,白的不出错就行) to do ( dis = ...
- wiz笔记
分享本地的wiz笔记到网上 , 成为博客 博客园 http://www.cnblogs.com/您的博客名称/services/metablogapi.aspx // region 图片里面的字段:从 ...
- android中Zing二维码扫描,二维码生成
Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求 ...
- TSM_ISSUE_123
dsmc 命令详解http://www-ik.fzk.de/~apel/html/adsm_manual.htmlTSM InfoCenterhttp://www-01.ibm.com/support ...
- greenplum表的distributed key值查看
greenplum属于分布式的数据库,MPP+Share nothing的体系,查询的效率很快.不过,这是建立在数据分散均匀的基础上的.如果DK值设置不合理的话,完全有可能出现所有数据落在单个节点上的 ...
- 初识unitest框架
1.借助IED录制脚本 2.导出脚本,选择用Python语言 将脚本导出,保存为 baidu.py ,通过 python IDLE 编辑器打开 引入unittest框架解释,见代码的的注释 # -*- ...