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. django Form数据读取问题

    1.在我学习django的过程中,我学习到了一个关于表单验证的问题 2.我们从前端post一个表单,通过urls配置,传给对应的view方法 3.然后再传给Form验证 4.一开始我是很好奇,在vie ...

  2. MonggoDB学习笔记

    MongoDB MongoDB介绍:非关系型的文档数据库.MongoDB的数据模型是面向文档的,文档是一种类似于JSON的结构.简单理解MongoDB这个数据库中存的是各种各样的JSON.(BSON) ...

  3. 详解exif.js,应用于canvas照片倒转(海报H5)

    业务背景,苹果手机调用上传接口拍照没有问题但是上传到网页上照片倒转了解决方法利用exif.js读取图片参数并对图片进行元数据修改 window.btoa(str)转码 window.atob(base ...

  4. thinkphp5+nginx的linux环境搭建

    安装环境&工具安装php安装nginx运行服务器安装thinkphp安装Composer安装thinkphp配置nginx.conf配置php-fpm运行thinkphp注意事项 php7已经 ...

  5. 转 Unity企业级支持案例与分析

    Unity大中华区技术支持总监张黎明以“Unity企业级支持案例与分析”为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加今年的Unite,其实我现在看到有的朋友已经不是第一次来参加Un ...

  6. npm update常用命令使用

    一.更新 npm-check检查更新 npm install -g npm-check npm-check 2. npm-upgrade更新 npm install -g npm-upgrade np ...

  7. BNU 33693——Problemsetting——————【枚举+最大流】

    Problemsetting Time Limit: 5000ms Memory Limit: 131072KB 64-bit integer IO format: %lld      Java cl ...

  8. svn update 报错,必须先cleanup,然后cleanup失败解决方法

    一 问题描述: 1.svn update失败,提示已被locked,请执行cleanup 2.执行svn cleanup,提示cleanup failed to process the followi ...

  9. SQL 文件以及文件组

    1.SQL Server根据分区表名查找所在的文件及文件组实现脚本 --SQL Server根据分区表名查找所在的文件及文件组实现脚本 SELECT fg.name AS FileGroupName ...

  10. 解决 Java 调用 Azure SDK 证书错误 javax.net.ssl.SSLHandshakeException

    Azure 作为微软的公有云平台,提供了非常丰富的 SDK 和 API 让开发人员可以非常方便的调用的各项服务,目前除了自家的 .NET.Java.Python. nodeJS.Ruby,PHP 等语 ...