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

第 14 章. 使用 pandas 操作Excel 文件

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

到目前为止,我们一直是以交互方式处理 Python 和 Excel 的。 我们运行 Python 脚本来自动化 Excel 操作,并将 Excel 工作簿作为用户界面来调用 Python 函数——无论是通过 xlwings 还是 Python in Excel。在第六部分中,我们将跳过 Excel 应用程序:我们将直接处理 Excel 文件,而无需在 Excel 中打开它们。在本章中,您将学习如何使用 pandas 读写 Excel 工作簿。 我们将首先回顾第 8 章中的报表案例研究。随后,我将更深入地介绍 pandas 用于处理 Excel 文件的工具:用于读取的read_excel函数和ExcelFile类,以及用于写入 Excel 文件的to_excel方法和ExcelWriter类。由于我们处理的是文件而非 Excel 应用程序,本章中的所有代码示例在 Python 运行的任何环境中均可运行,包括 Linux。

报表案例研究(第二版)

第8章的 案例研究中,我们使用pandasread_excel函数读取了配套代码库中sales_data目录下的所有源文件。在本版本的案例研究中,我们将采用相同的方法。不过,这次我们将不再使用xlwings将报告写入预格式化的模板,而是继续使用pandas,将DataFrame写入一个新的Excel文件。 由于 pandas 本身无法创建 Excel 图表,因此本版本的报告将仅包含表格。如示例 14-1 所示,Python 脚本sales_report_pandas.pysales_report_xlwings.py 几乎完全相同。区别在于,报告是通过脚本最后一行中的to_excelDataFrame 方法生成的。

示例 14-1. sales_report_pandas .py
from pathlib import Path

import pandas as pd

# Directory of this file
this_dir = Path(__file__).resolve().parent

# Read in all files
parts = []
for path in (this_dir / "sales_data").rglob("*.xls*"):
    print(f"Reading {path.name}")
    part = pd.read_excel(path, engine="calamine")
    parts.append(part)

# Combine the DataFrames from each file into a single DataFrame
df = pd.concat(parts)

# Pivot each store into a column and sum up all transactions per date
pivot = pd.pivot_table(
    df, index="transaction_date", columns="store", values="amount", aggfunc="sum"
)

# Resample to end of month and assign an index name
summary = pivot.resample("ME" ...
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