Skip to Content
Python for Excel (Chinese Edition), 2nd Edition
book

Python for Excel (Chinese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
5h 50m
Chinese
O'Reilly Media, Inc.
Content preview from Python for Excel (Chinese Edition), 2nd Edition

第 13 章. 使用 xlwings Lite创建自定义 函数

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

自定义函数是 Python 函数,您可以在 Excel 单元格中像使用SUMAVERAGE 等内置函数一样使用它们。如果这听起来很熟悉,那是因为我在第 11 章中已经介绍过自定义函数。不同之处在于,那些函数仅与 Windows 兼容,而使用 xlwings Lite 的自定义函数则可在 Windows、macOS 和 Web 版 Excel 上运行。

本章将从一个简单的“Hello World”函数开始,随后我将介绍一些现代化的改进,包括如何使用类型提示进行数据转换以及如何利用命名空间来组织自定义函数。之后,我们将逐步完成三个项目:

  • 构建一个 AI 函数,向您展示如何通过环境变量处理敏感凭据

  • 使用 Hugging Face 的机器学习模型,这将展示借助 Pyodide(驱动 xlwings Lite 的受限 Python 环境,参见上一章)能实现哪些功能

  • 处理 Open-Meteo 的气象数据,这将教你如何处理日期和图表

我们在第 11 章学到的内容大多也适用于 xlwings Lite 的自定义函数,但 xlwings Lite 利用了现代 Excel 功能,例如将图表作为单元格内图像返回。不过,让我们不要操之过急——先从基础开始吧!

现代自定义函数

在本节中,我将向您展示如何使用类型提示(而非装饰器)将 Excel 值转换为 DataFrame 及其他 Python 数据类型。我们还将探讨如何处理错误,以及如何将函数组织到命名空间中。但先从基础开始:让我们从熟悉的“Hello World”函数入手。

Hello World

要编写这个“Hello World”函数,请创建一个新工作簿,并通过点击Excel功能区“主页”选项卡上的按钮启动xlwings Lite。与上一章一样,在编辑器中通过Ctrl+A(Windows)或Command-A(macOS)全选默认示例代码并将其删除。然后编写以下代码:

from xlwings import arg, func

@func
def hello(name):
    return f"Hello {name}!"

等待main.py文件标签页中出现绿色勾号,这表示文件已保存。接着,要测试该函数,请选中单元格 A1,输入公式 =HELLO("xlwings"),并按下 Enter 键。如预期所示,该公式将计算结果为Hello xlwings!

自定义函数故障排除

如果您在调用#NAME?函数时遇到HELLO错误,说明您的函数尚未正确注册。这种情况偶尔会发生,通常可以通过清除 Office 缓存来解决,具体方法详见xlwings Lite 故障排除文档。如果此方法无效,且您安装了其他来自加载项商店的加载项,请先卸载所有加载项,然后再重新安装。

若将此版本的 Hello World 函数与第 11 章中的版本进行对比,你会发现一些差异:

  • 您无需更改任何 Excel 设置。

  • 无需手动导入函数——保存main.py后,函数即已就绪。

  • 当您输入 =HELLO(,会弹出一个工具提示,显示函数参数的名称。

  • 为了遵循各平台的语法约定,Excel 会自动将小写的 Python 函数名转换为大写。

既然你已经看到了最基本的自定义函数的实际应用,接下来将学习如何使用类型提示将 Excel 值转换为特定的 Python ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习勒索软件响应和恢复 (Chinese Edition)

学习勒索软件响应和恢复 (Chinese Edition)

W. Curtis Preston, Michael Saylor
Prometheus:快速入门,第二版

Prometheus:快速入门,第二版

Julien Pivotto, Brian Brazil

Publisher Resources

ISBN: 0642572396008