Automate Excel with Python From Manual Grind to One-Click Workflow (John Wengler)(Z-Library)
Technology
No Description
98
Views
0
Downloads
0.00
Total Donations
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
(This page has no text content)
Page
3
AUTOMATE EXCEL WITH PYTHON From Manual Grind to One-Click Workflow by John Wengler no starch press® San Francisco
Page
4
AUTOMATE EXCEL WITH PYTHON. Copyright © 2026 by John Wengler. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing 30 29 28 27 26 1 2 3 4 5 ISBN-13: 978-1-7185-0464-6 (print) ISBN-13: 978-1-7185-0465-3 (ebook) Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Jennifer Kepler Developmental Editor: Rachel Monaghan Cover Illustrator: Rob Fiore Interior Design: Octopod Studios Technical Reviewer: Daniel Zingaro Copyeditor: Ryan E. Holman Proofreader: Michael Fedison Indexer: BIM Creatives, LLC Library of Congress Control Number: 2026001077 For customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.com. For permission to translate this work: rights@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b-14, 11317 Tallinn, Estonia, hello@eucompliancepartner.com, +3375690241. No Starch Press and the No Starch Press iron logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked
Page
5
name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
Page
6
For Dragana, naturally.
Page
7
About the Author John Wengler enjoys a Mark Twain–like career spanning journalism, community planning, risk management, and compliance. He taught himself Python to upgrade a spreadsheet process to help an employer save millions on a third-party system. John wrote the 2001 book Managing Energy Risk, as well as dozens of industry articles on corporate governance. A distinguished public speaker, he has taught courses at the Illinois Institute of Technology and Tulane University. John helped create and write the Energy Risk Professional certification program for the Global Association of Risk Professionals. He is also a member of Anaconda’s Customer Advocacy Program. Prior to learning Python, John previously attempted coding as an undergrad at the University of Wisconsin, using punch cards in the basement mainframe neon dungeon of the civil engineering building. He somehow managed to avoid any and all programming when achieving a master’s degree in urban planning at the University of Illinois Chicago and an MBA from Northwestern University’s Kellogg School. John’s hobbies include writing, studying history, and spending time in the outdoors. Unlike Mark Twain, however, he has yet to captain a riverboat.
Page
8
About the Technical Reviewer Daniel Zingaro is a teaching professor at the University of Toronto. He co-directs the GenAI in CS Education Consortium (https://www.teachcswithai.org), aimed at helping faculty and institutions integrate generative AI into the computer science curriculum. He is the author of Algorithmic Thinking, 2nd edition (No Starch Press, 2023), and Learn to Code by Solving Problems (No Starch Press, 2021) and co-author of Learn AI-Assisted Python Programming (Manning, 2023). Visit https://www.danielzingaro.com for more information on these and his other programming books.
Page
9
BRIEF CONTENTS Acknowledgments Introduction PART I: FROM SPREADSHEETS TO DATAFRAMES Chapter 1: Getting Started with Python Chapter 2: Displaying Data and Understanding Data Types Chapter 3: Creating and Manipulating Dataframes and Lists Chapter 4: Adding, Modifying, and Calculating Column Data Chapter 5: Accessing and Transforming Individual Cell Values Chapter 6: Filtering and Displaying Dataframes PART II: TOOLS TO REPLICATE EXCEL FUNCTIONALITY Chapter 7: Counting and Summing Values Chapter 8: Merging Dataframes Chapter 9: Formatting and Calculating Dates and Times PART III: WORKFLOW TECHNIQUES Chapter 10: Reading Excel Files into Dataframes Chapter 11: Saving Dataframes to Excel Chapter 12: There and Back Again: An Excel–Python–Excel Workflow Appendix A: Working with Folders, Files, and Pathnames Appendix B: Cleaning Up a Messy Spreadsheet Appendix C: The Ducks Module Python Quick Reference Index
Page
10
CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Why This Book? Why Python? How This Book Is Organized Dataframes, Your New Best Friends PART I: FROM SPREADSHEETS TO DATAFRAMES 1 GETTING STARTED WITH PYTHON Technical Considerations for Working with Python Python Version Distribution Platform Integrated Development Environment The IDLE Shell: A Simple Interface Spyder: A More Robust Working Environment Summary 2 DISPLAYING DATA AND UNDERSTANDING DATA TYPES Printing String Variables and Strings of Strings Data Types Printing Numbers and Numeric Variables Concatenating Different Data Types Learning from Your Mistakes Customizing Functionality with Parameters
Page
11
Printing an Empty Line Escape Characters and Escape Sequences Potential Pitfalls for Python Novices Using Quotation Marks Inconsistently Ignoring Case Sensitivity Not Knowing Your Data Summary 3 CREATING AND MANIPULATING DATAFRAMES AND LISTS What Exactly Is a Dataframe? How to Create a Dataframe Importing the pandas Module and Manually Creating a Dataframe Copying a Dataframe Subsetting a Dataframe by Column Common Dataframe Operations Counting Rows Using the len() Function Counting Rows and Columns with the shape Attribute Deleting Rows with a Specific Value Identifying and Dropping Duplicated Rows Concatenating Dataframes Lists: The DNA of Dataframes Creating a List Isolating Unique List Values Appending a Single Item to a List Adding a List to a List Sorting a List Identifying Minimum, Maximum, and Mean List Values Removing a List Element Comparing Lists Creating an Empty Dataframe with a List
Page
12
Converting a Column into a List Another Fragment of Dataframe DNA: The Series Object Summary 4 ADDING, MODIFYING, AND CALCULATING COLUMN DATA Defining a Column Changing How Columns Appear in a Dataframe Reordering Columns in a Dataframe Dropping Columns Renaming Columns Sorting Dataframes by Column Values Printing Select Columns Changing Values in Columns Overwriting All Column Values Replacing Particular Values in Dataframes and Target Columns Replacing Substrings Creating New Columns Adding a New Column with a Single Value Duplicating a Column Concatenating Two or More Columns into a New One Math Methods and Operators Applying the sum() Method to a Dataframe Column Returning Average, Maximum, or Minimum Values Calculating Median Values Rounding a Single Column or Full Dataframe Converting to Absolute Values Stringing Together Multiple Methods Storing Calculated Values in a New Column Comparing Values Conditional Logic
Page
13
Controlling Execution Flow with if Logic Iterating Through a List Repeating a Process with the while Loop Iterating Through a Dataframe Replicating SUMIF with the where() Conditional Statement Storing Values Returned from iterrows() in a New Column Storing Conditional Results in a New Column with List Comprehensions Handling Exceptions with a try-except Block Summary 5 ACCESSING AND TRANSFORMING INDIVIDUAL CELL VALUES An Overview of Values and Variables Converting Integers, Floats, and Strings Converting Individual Variables Converting an Entire Dataframe Column Converting Objects to Strings Manually Inserting Values with the input() Function Answering a Question and Saving the Answer Selecting a Menu Option Pausing Execution to Review Output Working with NaN Objects Manually Creating NaN Objects Manually Entering NaN Objects into a Dataframe Replacing NaN Objects Slicing Techniques for Strings and Lists Slicing a Single String Slicing Within an Entire Column Slicing a List from a List Pulling a Select Element from a List Indexing Techniques for Dataframes and Series Objects
Page
14
Pulling Values by Index Position Pulling Values by Unique Index Label Targeting Single Values Pulling a Value from a Series Modifying Existing Cells Splitting Techniques Splitting a Single String Handling Inconsistent Delimiters Splitting Columns in a Dataframe Summary 6 FILTERING AND DISPLAYING DATAFRAMES A Closer Look at the DataFrame() Method Using Optional Arguments Creating an Empty Dataframe with a Column List Working with Dataframe Indexes Naming and Renaming an Index Renumbering or Resetting an Index Sorting by the Index Moving Values Between the Index and a Column Subsetting Dataframes By Column List By Relative Index Location By Index Label By Matching Values in Columns By Excluding Values in Columns By a Substring Value By Column Labels in a List By Inclusion By Exclusion By Mathematical Condition By NaN Objects
Page
15
Controlling the Appearance of Dataframe Output Printing the First or Last Few Rows Printing Specific Rows Printing the Rightmost Columns Customizing Global Display Settings Working with Dictionary Objects Declaring a Single Dictionary Declaring a List of Dictionaries Accessing and Modifying Dictionary Contents Creating New Dictionary Key-Value Pairs on the Fly Storing Dictionaries in Dataframes Summary PART II: TOOLS TO REPLICATE EXCEL FUNCTIONALITY 7 COUNTING AND SUMMING VALUES The value_counts() Method Counting Every Value in a Column Counting Specific Values in a Column Counting a List of Specific Values in a Column Normalizing value_counts() Results The crosstab() Method Creating Basic Cross-Tabulations Performing Math Operations Adding Row and Column Totals Handling Missing Values Normalizing crosstab() Data The pivot_table() Method Breaking Down the Basic Form of pivot_table() Grouping Unique Values Calculating Math Values Handling NaN Objects
Page
16
Organizing Dataframe Values Summary 8 MERGING DATAFRAMES The Basics of VLOOKUP and merge() How merge() Handles Orphaned Keys The Full merge() Method Syntax Specifying Join Type Analyzing Merge Results Defining Keys Handling Different Key Column Labels Checking Your Match Expectations Quality Control with the shape Attribute Summary 9 FORMATTING AND CALCULATING DATES AND TIMES Introducing the Datetime Module and datetime.now() Creating Datetime Objects Isolating Units of Time as Integers Converting Datetime Objects to Strings Transforming Timestamps Converting a Single Datetime Object to a Custom-Formatted String Isolating Units of Time as Strings Removing Leading Zeros from Single-Digit Time Elements Working with Time Durations: The Timedelta Object Comparing and Calculating Dates and Times Datetime Objects in Dataframes Dataframe Datetime Operations Using Directives to Customize to_datetime() Results Calculating Timedeltas in a New Column
Page
17
Subsetting a Dataframe Using Datetime Objects Summary PART III: WORKFLOW TECHNIQUES 10 READING EXCEL FILES INTO DATAFRAMES Creating or Downloading Your Excel Spreadsheet Introducing the read_excel() Method Importing a Specific Tab from a Workbook Importing All Tabs at Once Filtering Source Data Parsing Input Spreadsheets Dealing with More Complex Spreadsheets Setting the Column Labels for Your Dataframe Setting an Excel Column as the Dataframe Index Handling Hard Returns in Excel Data Reading in a CSV File Summary 11 SAVING DATAFRAMES TO EXCEL Simple Single-Tab Export Exporting a More Complex Dataframe Designating the Tab Name Excluding the Dataframe Index Freezing the View The Six Steps to Exporting and Formatting Excel Files Step 1: Creating a Writer Object and Excel File Step 2: Adding Multiple Dataframes to One Excel Workbook Step 3: Closing the Writer Object and Excel File Step 4: Creating and Populating the Workbook Object and Excel File
Page
18
Step 5: Formatting the Excel File Step 6: Closing the Workbook Object and Excel File Emailing from Python Sending a Basic Email Converting Dataframes to HTML Code Sending an Email Containing HTML Summary 12 THERE AND BACK AGAIN: AN EXCEL–PYTHON–EXCEL WORKFLOW The Scenario Analyzing the Vet’s Workbook Flowcharting the Manual Process Coding with Modularity Writing and Calling UDFs Defining Required and Optional Parameters Returning One or More Values Saving UDFs in a Separate Script Writing Your First Script: Rolling Over a File Importing Your Favorite Modules Importing the Ducks UDFs Printing a Header Setting Your Dataframe Display Preferences Setting the Date Creating a New File for Today’s Date Automating Exception Reports and Data Management Tasks Importing the Vet’s Workbook Tabs Generating and Sending an Email Automating the Overdue Invoice Process Filtering Today’s Appointments Pausing the Program’s Execution Automating Daily Appointment Email
Page
19
Recording New Appointments Exporting Dataframes to Excel Tabs Updating and Formatting the Excel File in Spyder Automating Updates in Excel Files Analyzing Trends with Dynamic Pivot Tables Using Spyder as Your Daily Workflow GUI Summary A WORKING WITH FOLDERS, FILES, AND PATHNAMES Pathnames in File Explorer vs. Python Viewing and Changing Your Working Directory Listing the Contents of a Folder Creating a New Directory Checking Whether a Pathname Exists Copying a File Renaming or Moving a File or Folder Deleting a File Deleting Folders File Management Quick Reference B CLEANING UP A MESSY SPREADSHEET Reading a Messy Spreadsheet into a Dataframe Customizing Column Labels Specifying a Column as the Index Dropping a Record by Index Label Sorting by Index Label Splitting Columns: Converting Full Names to First and Last Renaming Columns Reordering Columns Changing a Specific Cell Value Converting Strings to Datetime Objects
Page
20
Exporting the Cleaned-Up Dataframe Back to Excel Formatting the Clean Excel File The Final Product C THE DUCKS MODULE PYTHON QUICK REFERENCE INDEX
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Automate Excel with Python: From Manual Grind to One-Click Workflow
## 【One-Line Pitch】
A practical cookbook for Excel professionals who want to automate repetitive spreadsheet tasks using Python—without abandoning Excel. If you've ever wished you could turn a daily manual grind into a one-click workflow, this book shows you exactly how, step by step.
## 【Book Arc】
- **Opening (~0%–10%)**: Sets up the book's philosophy—you're not replacing Excel, you're adding Python to your workflow. Covers Python installation, choosing between IDLE and Spyder, and the absolute basics of printing, variables, and data types.
- **Early (~10%–23%)**: Introduces dataframes as "your new best friends"—the pandas object that looks and feels like a spreadsheet. Covers creating dataframes, lists, concatenation, and basic manipulation techniques like reordering columns.
- **Early-Middle (~23%–39%)**: Dives into data transformation—rounding, replacing values, string slicing (the Python equivalent of Excel's LEFT/RIGHT functions), and conditional logic with if/elif/else statements. Includes the crucial concept of iterating through lists and dataframes.
- **Middle (~39%–48%)**: Focuses on precision data access—using loc[], iloc[], at[], and iat[] to pull specific cells, working with series objects, and splitting columns into new columns (like Excel's Text-to-Columns feature).
- **Middle-Late (~48%–60%)**: Covers dataframe housekeeping—naming and renaming indexes, sorting, and the critical copy() method to avoid the "entangled dataframe" problem that causes SettingWithCopyWarning errors.
- **Late (~60%–100%)**: Moves to real-world automation—building complete workflows that read Excel files, process data, format output, and even send emails. Includes a full case study of automating a vet's daily appointment and invoicing processes.
## 【Key Takeaways】
- **Dataframes are the bridge between Excel and Python** (Early): The pandas dataframe looks like a familiar spreadsheet but offers programmatic power. Master this object and you've unlocked 80% of the book's value.
- **Python's print() function is your debugging friend** (Early): Learn to display variables, customize separators with sep=, and control line endings with end=. These simple techniques help you verify what your code is actually doing.
- **Lists are the building blocks of data manipulation** (Early): Python lists support adding, removing, and comparing elements. The remove() function only deletes the first occurrence—for multiple removals, you'll need loop logic.
- **String slicing replaces Excel's LEFT, RIGHT, and MID** (Middle): Python uses [start:stop] notation where stop is exclusive. For example, 'Chicago'[0:3] returns 'Chi'. Negative indices work from the end: 'Chicago'[-2:] returns 'go'.
- **at[] and iat[] are faster than loc[] and iloc[] for single cells** (Middle): When you need just one value, these optimized attributes provide quicker lookups—at[] by label, iat[] by position.
- **Always use copy() when creating dataframe aliases** (Middle): Without it, you're just creating another reference to the same object. Changes to one "copy" silently change the original—a common source of confusing bugs.
- **try/except blocks prevent crashes from messy data** (Early): Real-world spreadsheets have inconsistencies. Wrapping risky operations in try/except lets your script handle errors gracefully instead of dying mid-run.
- **Modularity makes automation maintainable** (Late): Save user-defined functions (UDFs) in separate scripts, import them as needed, and build workflows from reusable pieces rather than one monolithic script.
## 【Reading Tips】
- **Skim Chapters 1–2 if you've coded before**: The basics of print(), variables, and data types are standard fare. Jump ahead to Chapter 3 where dataframes are introduced—that's where the Excel-specific value begins.
- **Deep-read the dataframe chapters (3–6)**: These are the heart of the book. Pay special attention to the copy() method discussion and the index manipulation techniques—they'll save you hours of debugging later.
- **Use the book as a reference, not a novel**: The author explicitly designed this as a cookbook. When you hit a problem, flip to the relevant section rather than reading sequentially.
- **Work through the vet clinic case study (Chapter 12)**: This end-to-end example ties everything together—reading Excel files, processing data, generating reports, and sending emails. It's the best model for building your own automation.
- **Watch for the "Excel translation" moments**: The author frequently maps Python concepts back to Excel equivalents (like string slicing vs. LEFT/RIGHT). These comparisons are gold for Excel pros learning Python.
## 【Coverage Limits】
This guide covers the foundational and intermediate dataframe techniques visible in the excerpts (roughly the first half of the book). The later chapters on email automation, Excel formatting, and the full workflow case study are referenced but not detailed here—the excerpts don't provide enough depth on those sections.
##
Passage locations
Excerpt 1
on works the way it works, I provide multiple examples with the aim of helping you start solving problems right away. This book’s organization is based on so...
View in text
Excerpt 2
asic dataframe you saw in the book’s introduction; meet the dataframe’s close relative, the list; and learn how to perform several common operations on both...
View in text
Excerpt 3
e same logic to each element within that object. To iterate through a list, for example, you create a for statement that picks up each value in turn and appl...
View in text
Excerpt 4
item is itself a list containing the two name components ➍. Because the source series contained a list in each cell, the to_list() method returns a list of l...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay