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的更多相关文章

  1. 利用 Python 插件 xlwings 读写 Excel

    Python 通过 xlwings 读取 Excel 数据 去年底公司让我做设备管理,多次委婉拒绝,最终还是做了.其实我比较喜欢技术.做管理后发现现场没有停机率统计,而原始数据有,每次要自己在Exce ...

  2. 一个使用xlwings操作excel数据优化60倍处理效率的案例

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 老猿在将自己的博文数据(包括url地址.标题和阅读数量)从博客中获取后,使用xlwings保存到excel对象时发现,不同的处理方法性能相差非常大. ...

  3. Working with Excel Files in Python

    Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...

  4. 像Excel一样使用python进行数据分析

    Excel是数据分析中最常用的工具,本篇文章通过python与excel的功能对比介绍如何使用python通过函数式编程完成excel中的数据处理及分析工作.在Python中pandas库用于数据处理 ...

  5. python excel 像 Excel 一样使用 python 进行数据分析

    https://www.jb51.net/article/63216.htm 像 Excel 一样使用 python 进行数据分析 :  https://www.cnblogs.com/nxld/p/ ...

  6. [转]Introduction - Run Excel Macro using VBScript

    本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...

  7. 用python批量执行VBA代码

    先说下背景环境 1. 公司需要问卷调查,有两份问卷, 1)是spss问卷,2)是excel问卷.spss问卷数据不全,但有各种标签, excel呢, 生成的数据直接把选项变成了值 2. 现在需要把ex ...

  8. 个人永久性免费-Excel催化剂功能第47波-VBA开发者喜爱的加密函数类

    VBA的确是个很不错的编程工具,寄生在OFFICE内,无需安装庞大的开发环境,即开即用,方便灵活,能实现的事情也很多,但毕竟VBA是微软停止更新维护将近20年的一种语言,计算机的世界发展速度有多快大家 ...

  9. Python 使用 xlwings 往 excel 中写入一行数据的两种方法

    该方法跟上一篇写入一列的方法相反,代码如下: # -*- coding:utf-8 -*- import xlwings as xw list1 = [1,2,3,4,5] list2 = [[1], ...

随机推荐

  1. SMB共享

    [root@samba1 ~]# yum install samba samba-client  samba-common -y 因为只有真实存在的用户才能在samba中建立,所以我们新建两个测试用户 ...

  2. linux使用curl命令行进行接口测试

    cURL介绍cURL 是很方便的Rest客戶端,可以很方便的完成许多Rest API测试的需求,甚至,如果是需要先登入或认证的rest api,也可以進行测试,利用curl指令,可以送出HTTP GE ...

  3. HttpSessionListener

    1 知识点

  4. 【Lua】关于遍历指定路径下所有目录及文件

    关于Lua中如何遍历指定文件路径下的所有文件,需要用到Lua的lfs库. 首先创建一个temp.lua文件,用编辑器打开: 要使用lfs库,首先需要把lfs库加载进来 require("lf ...

  5. unity优化

    1. 更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种,每家的GPU支持不同的压缩格式,但他们都兼容ETC格式, 2. 对于透明贴图,我们只能选择RGBA 16 ...

  6. 游戏反编译工具dnSpy

    dnSpy使用的工具下载地址为: https://github.com/cnxy/dnSpy/archive/v4.0.0.zip 或 dnSpy官方下载地址: https://github.com/ ...

  7. android去除标题栏

    在 AndroidManifast.xml 文件中 将 theme="@style/AppTheme" 改为 theme="@style/Theme.AppCompat. ...

  8. step1: python & scrapy安装

    #首先安装python,这里安装python所需依赖包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...

  9. kd-tree 小结

    核心思想 是一种分割 \(k\) 维数据空间的数据结构 一维情况下就是平衡树,以 \(key\) 为标准判断插入左儿子还是右儿子 \(kdtree\) 就是平衡树在多维空间的扩展 因为有多维,我们按不 ...

  10. hexo+github搭建博客跳坑

    hexo+GitHub搭建博客过程中,hexo安装成功,可以启动和运行,但是访问localhost:4000却无法访问,弄了半天,最后发现是福昕阅读器占用了4000端口 解决办法: 采用命令hexo ...