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. Shiro如何使用Ehcache实现Session共享

    最近项目中用到的Session共享场景:两个独立应用,希望实现DB层共享用户,而且用户只需要登录一次. 分析:这种场合,不适用单点,因为用户数据并不需要单独在第三方应用管理,而且添加单点也会增加整个系 ...

  2. Ubuntu系统下移动宽带自动启用设置

    对于需要自动移动联网的Ubuntu系统(安装了4G移动手机卡的),步骤如下: . 在终端编辑自启动文件:sudo gedit /etc/rc.local 2. 在最后一句exit 0的上⾯面加上如下指 ...

  3. $bzoj1027-JSOI2007$ 合金 计算几何 最小环

    题面描述 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金 ...

  4. python+selenium打开浏览器

    Firefox(高版本要安装换火狐驱动,47版本以下可不安装) GoogleChrome(需要安装浏览器的驱动插件,驱动到selenium官网下载,目前该浏览器的驱动只有32位的,所以Google安装 ...

  5. Kubernetes Running Locally

    1. Requirements 1) Linux Not running Linux? Consider running Minikube, or on a cloud provider like G ...

  6. step3: 创建jobbole爬虫

    scrapy startproject Redbacktestcd Redbacktest 创建jobbole爬虫 scrapy genspider jobbole2 blog.jobbole.com ...

  7. Jmeter进行性能测试时多台负载机的配置方法

    参考:https://blog.csdn.net/russ44/article/details/54729461 Jmeter进行性能测试时多台负载机的配置方法 Jmeter 是java 应用,对于C ...

  8. Hadoop实战之二~ hadoop作业调度详解(1)

    对Hadoop的最感兴趣的地方,也就在于Hadoop的作业调度了,在正式介绍如何搭建Hadoop之前,深入理解一下Hadoop的作业调度很有必要.我们不一定能用得上Hadoop,但是如果理通顺Hado ...

  9. JavaScript对象中的this属性

    this属性表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用appl ...

  10. 05.部分类 partial

    namespace _06.部分类 { class Program { static void Main(string[] args) { } } /// <summary> /// 这个 ...