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

第 15 章. 使用 Reader 和 Writer 包操作Excel 文件

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

本章将向您介绍 reader 和 writer 包。它们可以直接读写 Excel 文件,而无需安装 Excel。当您调用read_excelto_excel函数时,pandas 会在后台使用这些包。直接使用 reader 和 writer 包,您可以创建更复杂的 Excel 报表。 此外,如果您在某个项目中仅需读写 Excel 文件,而无需使用 pandas 的其他功能,那么安装完整的 pandas 套件可能有些大材小用。 本章首先概述可用的包,然后学习它们的工作原理。之后,我们将探讨将 DataFrame 写入 Excel 时的样式设置。最后,我们将再次回顾报表案例研究。与上一章一样,本章无需安装 Excel,这意味着所有代码示例均可在 Windows、macOS 和 Linux 上运行。

读写 Excel 文件

存在许多能够读写 Excel 工作簿的 Python 包。在本节中,我将概述本章将要涉及的这些包。不过,为了避免让人感到不知所措,我将根据您是想读取、写入还是编辑 Excel 工作簿,先给出一些简单的建议:

阅读

请使用 `python-calamine` 或 `fastexcel`。这两个包都是基于 `calamine` 构建的,而 `calamine` 是一个用 Rust 编写的库。最佳使用方式是通过 pandas 或 Polars(参见“现代替代方案”)并设置engine="calamine",但如果你不需要 DataFrame,也可以单独使用 `python-calamine`(fastexcel 计划在 1.0 版本中推出独立运行模式)。Polars 默认使用 calamine 引擎,但在 pandas 中,您必须像本书中一直所做的那样显式设置:

pd.read_excel("filename.xlsx", engine="calamine")

虽然 pandas 使用 Python-calamine 作为其 Calamine 引擎,但 Polars 使用的是 fastexcel。我将在下一节中介绍这两个包。

写作

除非遇到限制,否则请使用 XlsxWriter——若遇到限制,请尝试使用 OpenPyXL 或 Excelize 解决。我推荐 XlsxWriter 的理由如下:

  • 这是最快的写入器。虽然 XlsxWriter 是用 Python 编写的,但现在同一位作者还推出了一个基于 Rust 的 XlsxWriter 项目。未来,它很可能会被一个 Python 包封装,其速度将比当前的纯 Python 实现快数倍。

  • 文档非常完善。

  • 它正在积极维护中。

  • 它生成的 Excel 工作簿完全兼容。

使用 pandas 写入引擎

pandas使用,具体采用XlsxWriter还是OpenPyXL作为写入引擎,取决于你安装了哪个包。如果你同时安装了这两个包,默认使用XlsxWriter。如果你想改用OpenPyXL,请显式engine

df.to_excel("filename.xlsx", engine="openpyxl")

请注意,Polars仅使用XlsxWriter作为其写入引擎。

编辑

请使用 OpenPyXL,因为它是处理现代 Excel 文件的唯一解决方案。然而,由于 Excel 工作簿由多个相互依赖的文件组成,编辑是一项复杂的任务。因此,OpenPyXL ...

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