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

第 6 章. 在 Excel 中入门 Python

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

到目前为止,您已经对 pandas DataFrame 和 Jupyter Notebook 有了相当程度的了解,这使您具备了探索 Excel 中 Python 的绝佳基础。为什么呢?因为 Excel 中的 Python 允许您将 Excel 单元格转换为 Jupyter Notebook 单元格。这意味着,Excel 中的 Python 主要在于通过 pandas DataFrame 和 Python 绘图功能,获取更强大的分析和图表引擎。 不过,它并不与 VBA 或 Office 脚本形成竞争,因为它不支持 Excel 自动化或自定义函数——即那些可直接从 Excel 单元格调用的函数,例如=SUM()

本章将向您介绍 Excel 中 Python 的基本概念:您将学习如何在 Excel 单元格中编写 Python 代码,以及如何将 Excel 数据范围转换为 pandas DataFrame,并再次转换回原生 Excel 值。您还将了解 Excel 中 Python 附带的特定工具,例如 Python 编辑器。但在开始之前,让我们先确保您具备在 Excel 中运行 Python 的必要条件!

系统要求

尽管名为“”,但 Excel 中的 Python 实际上运行在微软的 Azure 云端,而非 Excel 本身:每次您运行包含 Python 代码的单元格时,Excel 都会将相关的电子表格数据发送至云端,Python 代码在云端运行,并将结果发回 Excel。这导致了以下要求:

  • 在 Excel 中使用 Python 需要互联网连接。

  • Excel 中的 Python 需要 Microsoft 365 订阅,且不支持带有永久许可证的 Office 版本(例如 Office 2024)。

Python 源代码

虽然 Excel 中的 Python 是在 Cloud 上运行您的代码,但代码本身存储在 Excel 文件内。

要确认您是否可以使用 Excel 中的 Python,请转到功能区菜单中的“公式”选项卡:您是否看到了如图 6-1 所示的“Python”组?如果未看到,请参考微软文章《Excel 中 Python 的可用性》确认是否满足要求,并更新您的 Excel 安装。如果看到了:太好了,您已准备就绪,可以开始编写 代码了!

Screenshot of the Excel ribbon showing the Formulas tab with the Python group, including options for Insert Python, Reset, Editor, and Initialization.
图 6-1. Excel 功能区“公式”选项卡中的 Python 菜单

在 Excel 中编写 Python 代码

在本节中,您将了解到 Python 单元格有两种输出类型(Python 对象和 Excel 值),并且xl函数用于读取 Excel 单元格的值。我们还将创建图表并使用 Python 编辑器。不过,要完成所有这些操作,您首先需要知道如何将普通的 Excel 单元格转换为 Python 单元格。

Python单元格

要在 Excel 单元格中编写Python 代码,首先需将其转换为 Python 单元格:打开 Excel 并创建一个新工作簿,然后选中单元格 A1。现在输入 =py 并按下 Tab 键。这将把单元格 A1 转换为 ...

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