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.

时间: 2024-10-13 02:21:29

xlwings: Write Excel macro using python instead of VBA的相关文章

像Excel一样使用python进行数据分析(3)

7,数据汇总 第七部分是对数据进行分类汇总,Excel中使用分类汇总和数据透视可以按特定维度对数据进行汇总,python中使用的主要函数是groupby和pivot_table.下面分别介绍这两个函数的使用方法. 分类汇总 Excel的数据目录下提供了"分类汇总"功能,可以按指定的字段和汇总方式对数据表进行汇总.Python中通过Groupby函数完成相应的操作,并可以支持多级分类汇总. Groupby是进行分类汇总的函数,使用方法很简单,制定要分组的列名称就可以,也可以同时制定多个列

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

Excel是数据分析中最常用的工具,本篇文章通过python与excel的功能对比介绍如何使用python通过函数式编程完成excel中的数据处理及分析工作.在Python中pandas库用于数据处理 ,我们从1787页的pandas官网文档中总结出最常用的36个函数,通过这些函数介绍如何通过python完成数据生成和导入,数据清洗,预处理,以及最常见的数据分类,数据筛选,分类 汇总,透视等最常见的操作. 文章内容共分为9个部分.这是第一篇,介绍前3部分内容,数据表生成,数据表查看,和数据清洗.

Working with Excel Files in Python

Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to the best information available about working with Excel files in the Python programming language. The Packages There are python packages available to

用python批量执行VBA代码

先说下背景环境 1. 公司需要问卷调查,有两份问卷, 1)是spss问卷,2)是excel问卷.spss问卷数据不全,但有各种标签, excel呢, 生成的数据直接把选项变成了值 2. 现在需要把excel的选项值变成1, 2这种数字{1:“满意”} 3. 妹子已经把vba写好了,虽然一堆错误,我百度了半天,改了很多:( 由于需要执行很多vba命令,我就用py的字符串拼接,生成了,900多份,怀疑vba的代码有问题,凭直觉,但我不会vba,又懒得查,只能让cpu去做重复工作了 需要模块win32

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

VBA的确是个很不错的编程工具,寄生在OFFICE内,无需安装庞大的开发环境,即开即用,方便灵活,能实现的事情也很多,但毕竟VBA是微软停止更新维护将近20年的一种语言,计算机的世界发展速度有多快大家有目睹,今天抛砖引玉,拿几个加密类函数来讲说一下VSTO较VBA开发的一些优势所在. 文章出处说明 原文在简书上发表,再同步到Excel催化剂微信公众号或其他平台上,文章后续有修改和更新将在简书上操作, 其他平台不作同步修改更新,因此建议阅读其他出处的文章时,尽可能跳转回简书平台上查看. 题外话 本

Creating Excel files with Python and XlsxWriter——Introduction

XlsxWriter 是用来写Excel2007版本以上的xlsx文件的Python模块. XlsxWriter 在供选择的可以写Excel的Python模块中有自己的优缺点. #--------------------------------------------------------------------------------------- 优点: 1. 它支持相比其他模块而言更多的Excel 特征: 2. 它生成的Excel文件有很高的保真度,大多数情况下生成的文件和Excel生成

Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)

我们在项目中可能用到excel表生成,下面的代码就是对excel表的操作: 1 import xlsxwriter 2 import datetime 3 4 class write_excel(): 5 def __init__(self,path): 6 now = datetime.datetime.now() 7 fname = 'TestReport' + now.strftime("%Y-%m-%d") 8 self.row = 0 9 self.xl = xlsxwrit

Excel Macro宏 - 指定路径下两个TXT文件Copy后,导入Excel中并进行对比

由于Excel格式粘贴没时间,只写关键的VBA代码和TXT文档Format示例‘ Attribute VB_Name = "CopyAndDiffer" '2015/06/24 ADD By MAOUIZAYOI Option Explicit ' Excute Interface.Sub CopyAndDiffer() ' define variables    Dim ccnSht As Object       'ccnSht is named for current conso

python 合并多个excel(基于python 3.X)

# 下面这些变量需要您根据自己的具体情况选择title = ["编号","项目负责人","项目","报销事由","报销项内容*","费用合计","预约报销日期","报销人","结算信息"]# 在哪里搜索多个表格# filelocation = "E:\\totally\\FinancePDF"fileloca