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

第 10 章 Python 包追踪器

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

在本章中,我们将创建一个典型的商业应用程序,该程序从互联网下载数据,将其存储在数据库中,然后在 Excel 中进行可视化展示。这将帮助您理解 xlwings 在此类应用程序中扮演的角色,并让您看到使用 Python 连接外部系统是多么简单。 为了构建一个既贴近实际应用又相对简单易懂的项目,我设计了“Python 包追踪器”——这是一个 Excel 工具,用于显示特定 Python 包的年度发布次数。尽管这只是一个案例研究,但您可能会发现该工具确实有助于判断某个 Python 包是否正在积极开发中。

在熟悉该应用程序之后,我们将探讨几个理解其代码所必需的主题:在学习 Python 中的异常处理(这是应用程序开发中的重要概念)之前,我们将了解如何从互联网下载数据以及如何与数据库交互。 完成这些准备工作后,我们将逐一解析 Python Package Tracker 的各个组件,了解它们如何协同工作。本章最后,我们将探讨 xlwings 代码的调试机制。与前两章一样,本章要求你在 Windows 或 macOS 系统上安装 Microsoft Excel。现在,让我们开始试用 Python Package Tracker 吧!

亲身体验

要从用户角度理解包追踪器的工作原理,最好的方法莫过于亲自试用一番。请前往配套代码库,您会发现一个名为 packagetracker的文件夹。该文件夹中包含几个文件,但目前只需打开 Excel 文件packagetracker.xlsm 即可。

启用宏

打开支持宏的工作簿(xlsm扩展名)时,请确保通过 `` 正确启用宏,例如 `packagetracker.xlsm`:

  • 点击“启用内容”。

  • 处理来自互联网的xlsm文件时,你还必须额外点击“启用编辑”。

  • 不过在打开工作簿之前,您可能需要先解除文件的阻止状态:在 Windows 资源管理器中右键单击该文件,选择“属性”。在“常规”选项卡中勾选“解除阻止”复选框,然后点击“确定”退出属性窗口(macOS 系统无需解除文件阻止)。

现在,请转到“Database”工作表:我们需要先向数据库中导入一些数据,以便进行操作。如图 10-1 所示,输入一个包名,例如 xlwings,然后点击“添加包”。您可以选择Python 软件包索引(PyPI) 上存在的任何包名。

macOS:确认文件夹访问权限

在 macOS 上添加第一个包时,您需要确认一个弹出窗口(“Microsoft Excel.app 希望访问其他应用程序的数据”),以便应用程序能够访问packagetracker文件夹。

The screenshot shows a database sheet interface for adding a new package, with instructions to type a package name, like "xlwings," and click "Add Package" to update from PyPI.
图 10-1. Python 包跟踪器(数据库工作表)

如果一切按计划进行,您会在输入包名称的右侧看到“已成功添加 xlwings”的提示。此外,在“更新数据库”部分下方会显示“最后更新”时间戳,而“日志”部分也会更新,显示已成功下载 xlwings 并将其存储到数据库中。 让我们再操作一次,添加 pandas 包以便获取更多数据进行探索。现在,切换到“Tracker”工作表,在单元格 B5 的下拉菜单中选择 xlwings,然后点击“显示历史”。此时您的屏幕应与 ...

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