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

第 9 章 Python 驱动的 Excel 工具

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

在上一章中,我们学习了如何编写 Python 脚本以实现 Microsoft Excel 的自动化。虽然这非常强大,但用户必须能够熟练使用终端、VS Code 或 Jupyter 笔记本来运行这些脚本。如果您将这些工具提供给业务用户,情况很可能并非如此。对于他们来说,您需要隐藏 Python 部分,使 Excel 工具再次感觉像一个普通的启用了宏的工作簿。 本章将向您展示如何利用 xlwings 实现这一目标。我将首先介绍在 Excel 中运行 Python 代码的最简方法,然后探讨部署 xlwings 工具所面临的挑战——这也将使我们能够更详细地了解 xlwings 提供的配置选项。与上一章一样,本章要求您在 Windows 或 macOS 系统上安装了 Microsoft Excel。

将 Excel 用作前端

前端是指应用程序中用户可见并可与其交互的部分。前端的其他常见名称包括图形用户界面(GUI)或简称为用户界面(UI)。当我询问xlwings用户为何选择Excel而非现代Web应用时,答案通常是:“Excel是我们用户所熟悉的界面。” 依托电子表格单元格,用户能够快速直观地输入数据,这往往比使用功能不完善的网页界面更能提升工作效率。 本节首先将向您介绍 xlwings 加载项和 xlwings CLI(命令行界面),随后通过quickstart命令创建我们的第一个项目。最后,我将向您展示从 Excel 调用 Python 代码的两种方法:点击加载项中的“运行主程序”按钮,以及使用 VBA 中的RunPython函数。让我们从安装 xlwings 加载项开始吧!

xlwings 加载项

若要让 在Excel中调用Python脚本,您需要安装Excel加载项,或将工作簿设置为独立运行模式。我将在“部署”章节中介绍独立运行模式,因此本节重点介绍加载项。要安装加载项,请在终端中运行以下命令:

Windows(终端)

cd %userprofile%\python-for-excel-2e
uv run xlwings addin install

macOS(终端)

cd ~/python-for-excel-2e
uv run xlwings addin install

重新配置现有的 xlwings 加载项

若您在 Excel 中已通过先前安装拥有 xlwings 加载项,请额外运行以下命令,将加载项指向我们在第 2 章中创建的 Python 环境: uv run xlwings config create --force

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