Page
1
(This page has no text content)
Page
2
Python Automation Cookbook Third Edition 100+ new and updated recipes for scalable workflows, MCP integrations, and AI-powered automation Jaime Buelta
Page
3
Python Automation Cookbook Third Edition Copyright © 2026 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Portfolio Director: Kunal Chaudhari Relationship Lead: Denim Pinto Project Manager: Ankit Maroli Content Engineers: Alexander Powell, Deepayan Bhattacharjee Technical Editor: Irfa Ansari Indexer: Manju Arasan Production Designer: Shantanu Zagade First published: September 2018 Second edition: May 2020 Third edition: June 2026 Production reference: 1290626 Published by Packt Publishing Ltd. Grosvenor House 11 St Paul's Square Birmingham B3 1RB, UK. ISBN 978-1-80670-289-3 www.packtpub.com
Page
4
To my grandfather Joaquín, who taught me how difficult and rewarding it is to write a book – Jaime Buelta
Page
5
Contributors About the author Jaime Buelta has been a professional programmer for more than two decades, with the majority of that time spent working in Python. His career spans many different industries, including aerospace, networking, industrial systems, video game online services, finance, cybersecurity and AI. Across these roles, he has worked closely with non-technical areas such as marketing, management, sales and game design. A committed advocate of automating everything, so that computers or AI handle the heavy lifting and people can focus on what really makes them unique, he is the author of several books since 2018, including this one, Hands-On Docker for Microservices with Python, and Python Architecture Patterns. He currently lives in Dublin, Ireland, where he works as a Principal Software Engineer and is a long-time participant at PyCon Ireland. Writing a book is a far from solo endeavor. I want to thank the reviewers and the team at Packt, whose comments and feedback have greatly improved this new edition. I also want to thank the whole Python developer community, which is so welcoming and supportive and has created the best all- around programming language. But most importantly, I could not have done it without the support and patience of my beloved wife, Dana.
Page
6
About the reviewers Miklós Prisznyák is a software engineer with a research background and a technology entrepreneur based in Edinburgh. A physicist by training, he has over twenty years of experience across finance, media, and scientific computing. Since moving to the UK in 2018, he has worked as a senior Python developer for major institutions: a first role in Cambridge at an engineering company, several years in Glasgow at JP Morgan Chase, remote work on trading systems for Bank of America, and AWS serverless backends for Sky in Edinburgh. More recently his focus has moved to AI. He worked with the Tanzanian Communications Regulatory Authority to build Big Data capability. He advised a public sector client in Tanzania and helped CRDB, Tanzania's largest commercial bank, with daily reconciliation of over USD 2 million in ATM transactions and with cash forecasting. He is co-founder and CEO of ADB Technology, which builds sovereign AI technology for African governments and enterprises, including the AISee media intelligence platform, bilingual English and Swahili voice agents, and clinical AI; he also co-founded Voith AI, building an AI clinical assistant. Miklós likes to travel and read, and he is interested in the sciences, mathematics, linguistics, history, politics, chess, and go (the board game). He still reads physics for pleasure, and enjoys a good cup of coffee. Above all, he values time with his son Zsombor, who is studying Computer Science in Edinburgh. Ricardo Bánffy is a software engineer and architect based in Dublin, Ireland, with 25+ years of building things in Python and a long list of other languages picked up along the way. He works as a senior engineer and technical leader, wrangling cloud platforms and automating workflows to help teams ship cleaner code faster. He's contributed to open-source projects like Debian, Fedora, Sysdig, and tmux. He also created the 3270font, a version of the classic IBM terminal typeface for modern computers. When he's not automating something, he's usually necromancing an old computer back to life.
Page
7
(This page has no text content)
Page
8
Table of Contents Preface xiii Free benefits with your book .............................................................................. xix Chapter 1: Let's Begin Our Automation Journey 1 Technical requirements ........................................................................................ 2 The elephant in the room: vibe coding .................................................................. 2 Working with multiple Python versions and virtual environments through uv ....... 5 There's more • 9 Installing third-party packages ............................................................................. 9 Creating strings with formatted values ................................................................ 13 Manipulating strings ........................................................................................... 17 Extracting data from structured strings .............................................................. 22 Using a third-party tool—parse .......................................................................... 26 Introducing regular expressions .......................................................................... 31 Going deeper into regular expressions ................................................................. 37 Adding command-line arguments ....................................................................... 43 Chapter 2: Automating Tasks Made Easy 51 Technical requirements ...................................................................................... 52 Preparing a task ................................................................................................. 52 Setting up a cron job ........................................................................................... 59 Capturing errors and problems ........................................................................... 65 Sending email notifications ................................................................................. 71 Chapter 3: Building Your First Web Scraping Application 77 Technical requirements ...................................................................................... 78 Downloading web pages ..................................................................................... 78
Page
9
Parsing HTML .................................................................................................... 82 Crawling the web ............................................................................................... 86 Subscribing to feeds ........................................................................................... 93 Accessing web APIs ............................................................................................. 97 Interacting with forms ..................................................................................... 102 Using Playwright for advanced interaction ........................................................ 109 Accessing password-protected pages with basic authentication .......................... 118 Accessing websites with browser cookies ........................................................... 121 There's more... • 124 Speeding up web scraping ................................................................................. 126 Chapter 4: Searching and Reading Local Files 133 Technical requirements ..................................................................................... 134 Crawling and searching directories .................................................................... 134 Reading text files ............................................................................................... 139 Dealing with encodings ..................................................................................... 142 Reading CSV files ............................................................................................... 147 Reading log files ................................................................................................ 151 Reading file metadata ........................................................................................ 154 Reading images ................................................................................................. 157 Reading PDF files ............................................................................................... 164 Reading Word documents .................................................................................. 169 Scanning documents for a keyword .................................................................... 173 Chapter 5: Generating Fantastic Reports 179 Technical requirements .................................................................................... 180 Creating a simple report in plain text ................................................................ 180 Using templates for reports ................................................................................ 183 Formatting text in Markdown ........................................................................... 188 Writing a basic Word document ......................................................................... 192 Styling a Word document .................................................................................. 196 Generating structure in Word documents .......................................................... 200 Table of Contents viii
Page
10
Adding pictures to Word documents ................................................................. 207 Writing a simple PDF document ........................................................................ 211 Structuring a PDF .............................................................................................. 215 Aggregating PDF reports ................................................................................... 222 Watermarking and encrypting a PDF ................................................................ 224 Chapter 6: Fun with Spreadsheets 231 Technical requirements .................................................................................... 232 Writing a CSV file ............................................................................................. 232 Updating CSV files ............................................................................................ 235 Reading an Excel spreadsheet ........................................................................... 238 Updating an Excel spreadsheet ......................................................................... 242 Creating new sheets in an Excel spreadsheet ..................................................... 245 Creating charts in Excel .................................................................................... 249 Working with cell formats in Excel .................................................................... 253 Using Python in Excel ....................................................................................... 259 Chapter 7: Cleaning and Processing Data 267 Technical requirements .................................................................................... 268 Preparing a CSV spreadsheet ............................................................................. 268 Appending currency based on location ............................................................... 273 Standardizing the date format .......................................................................... 278 Transforming and validating data in JSON ........................................................ 285 Aggregating results ........................................................................................... 293 Process data in parallel ..................................................................................... 300 Process data with Pandas .................................................................................. 307 Chapter 8: Developing Stunning Graphs 317 Technical requirements ..................................................................................... 318 Plotting a simple sales graph .............................................................................. 318 Drawing stacked bars ........................................................................................ 323 Displaying multiple lines .................................................................................. 328 ix Table of Contents
Page
11
Drawing a scatter plot ....................................................................................... 333 Visualizing map info ......................................................................................... 338 Adding legends and annotations ....................................................................... 347 Combining graphs ............................................................................................. 353 Saving charts ..................................................................................................... 357 Plotting a chart with plotly ............................................................................... 360 Chapter 9: Dealing with Communication Channels 367 Technical requirements .................................................................................... 368 Working with email templates .......................................................................... 368 Sending an individual email .............................................................................. 375 Reading an email .............................................................................................. 380 Adding subscribers to an email newsletter ........................................................ 384 Sending notifications via email ......................................................................... 390 Producing SMS messages .................................................................................. 395 Receiving SMS messages ................................................................................... 400 Creating a Telegram bot .................................................................................... 410 Chapter 10: Why Not Automate Your Marketing Campaign? 419 Technical requirements .................................................................................... 420 Detecting opportunities ................................................................................... 420 Creating personalized coupon codes ................................................................. 428 Sending a notification to a customer on their preferred channel ........................ 434 Preparing sales information ............................................................................... 441 Generating a sales report .................................................................................. 447 Chapter 11: Calling AI Models 455 Technical requirements .................................................................................... 456 Understanding GenAI elements ........................................................................ 458 Many different AI models • 459 Creating your own chatbot ................................................................................ 461 There's more… • 465 Table of Contents x
Page
12
Using a local model ........................................................................................... 467 There's more… • 471 Generating images ............................................................................................ 474 There's more… • 477 Describe images ............................................................................................... 479 There's more… • 483 Chapter 12: Adding Tools to GenAI 489 Technical requirements ..................................................................................... 491 Enhancing AI models with web search ............................................................... 491 There's more… • 495 Let's use MCP servers ....................................................................................... 496 There's more… • 502 Create your own MCP server ............................................................................. 506 There's more… • 513 Chapter 13: Building AI Agents for Business Automation 519 Technical requirements .................................................................................... 520 Prepare meetings by combining data from different tools .................................. 520 There's more… • 532 Draft documents with subagents ...................................................................... 534 There's more… • 542 Chapter 14: Real-World AI Example 545 Technical requirements .................................................................................... 546 Summarize your email and draft responses agentically ...................................... 546 Interact with the email service • 549 Retrieve sent emails • 552 Obtain the last day of received emails • 554 Create draft replies • 555 Send the summary email • 558 The agent that calls the tools • 559 xi Table of Contents
Page
13
There's more… • 565 Chapter 15: Automatic Testing Routines 569 Technical requirements ..................................................................................... 571 Writing and executing test cases ........................................................................ 571 Testing external code ......................................................................................... 575 Testing using dependency mocking .................................................................. 578 Testing using HTTP call mocking ...................................................................... 585 Preparing testing scenarios ............................................................................... 593 Running tests selectively ................................................................................. 600 Understand the coverage of your tests ............................................................... 607 Chapter 16: Debugging Techniques 615 Technical requirements .................................................................................... 618 Learning Python interpreter basics ................................................................... 618 Debugging through logging ............................................................................... 621 Debugging with breakpoints ............................................................................ 626 Improving your debugging skills ....................................................................... 632 Final thoughts ................................................................................................... 641 Chapter 17: Unlock Your Exclusive Benefits 643 Unlock this Book's Free Benefits in 3 Easy Steps ................................................. 644 Other Books You May Enjoy 648 Index 651 Table of Contents xii
Page
14
Preface We all spend too much time at work on manual tasks that add relatively little value. Scanning through information sources for a few relevant bits of data, updating the same spreadsheet graph with fresh data over and over, or opening files one by one to find the data we need. Many of these tasks can be automated so that the computer performs most of the work, but perhaps you don't yet feel capable of doing it. The good news is that now is the best moment to start. The Python language and its ecosystem have grown ever more powerful while remaining approachable. The environment continues to improve, with stronger integrations, richer libraries, and with a vibrant community behind it all. But the most impactful change since the previous editions of this book has been the rise of AI tools, which have become astonishingly capable within a very short space of time. Their progress over the past year in particular has been remarkable. AI tools can now help you create code, accelerate your learning, and even handle tasks independently. AI, however, is a multiplier. If your knowledge is solid, it will greatly amplify your abilities. But without clear direction, results will be scattered and unfocused. We will start the book by building our foundation, by way of small, clear recipes that can be adapted to your needs. These building blocks will gradually combine into more complex workflows. While we will note where AI can help us, we will focus first on the capabilities of Python, ensuring predictable, deterministic results. Among other recipes, you will learn to automate common tasks like scraping the web for opportunities, generating spreadsheet reports with graphs, sending automatic emails, receiving text notifications, and running tasks in the background, all while you focus on more important work. Don't underestimate this part! You will be achieving genuinely impressive results. Then we will build on this foundation by showing you how to work directly with AI models and create your own AI agents that combine some of the earlier recipes. The automations that result are powerful, but we don't shy away from discussing their limitations, caveats, and risks. Throughout the book we introduce many of the excellent third-party tool options to be found in the rich Python ecosystem. This will inspire your imagination and plant ideas for tackling a wide range of projects by leveraging the available libraries, even those not covered here. There are plenty to choose from!
Page
15
Although familiarity with Python is helpful, this book is designed for non-programmers. It offers clear, practical recipes, guiding you towards greater proficiency while keeping the focus on real, everyday goals. Who this book is for This book is for Python beginners, not necessarily developers, who want to use and expand their knowledge in order to automate tasks and learn how to do so with the help of AI. Most of the examples in the book are aimed at marketing, sales, and other non-tech areas. However, if you have already learnt a little about the Python language, such as its basic concepts, then so much the better. What this book covers Chapter 1, Let's Begin Our Automation Journey, presents some content that will be used all through the book. It addresses the 'vibe coding' idea, and explains how to install and manage third-party tools, how to perform effective string manipulation, and how to use command-line arguments. It also introduces you to regular expressions and other methods of text processing. Chapter 2, Automating Tasks Made Easy, shows how to prepare and automatically run tasks. It covers how to program tasks to be executed when they should, instead of running them manually; how to be notified with the result of a task that's run automatically; and how to be notified if there has been an error in an automated process. Chapter 3, Building Your First Web Scraping Applications, explores sending web requests to communicate with external websites, including via raw HTML content, structured feeds, and RESTful APIs; and looks at automating a browser to execute steps without manual intervention. It also covers how to process results to extract relevant information. Chapter 4, Searching and Reading Local Files, explains how to search through local files and directories and analyze the information stored there. You will learn how to filter through relevant files in different encodings and read files in several common formats, such as CSVs, PDFs, Word documents, and even images. Chapter 5, Generating Fantastic Reports, looks at how to display information given in text format in multiple formats. This includes creating templates to produce text files, as well as creating richly formatted and properly styled Word and PDF documents. Chapter 6, Fun with Spreadsheets, explores how to read and write spreadsheets in the CSV format, and in Microsoft Excel, including with formatting and charts and using Python inside Excel. Preface xiv
Page
16
Chapter 7, Cleaning and Processing Data, presents techniques to deal with multiple sources of information and clean up data before processing it. You will learn how to batch-process to speed up working with large amounts of data, including using specific data analysis libraries like Pandas. Chapter 8, Developing Stunning Graphs, explains how to produce beautiful charts, including common examples such as pie, line, and bar charts, as well as other advanced cases, such as stacked bars and even maps. It also explains how multiple graphs can be combined and styled to generate rich graphics and show relevant information in an understandable format. Chapter 9, Dealing with Communication Channels, explains how to send messages via multiple channels, using external tools to do the most of the heavy lifting. This chapter goes into sending and receiving emails individually as well as en masse, communicating through SMS messages, and creating a bot in Telegram. Chapter 10, Why Not Automate Your Marketing Campaign?, combines the different recipes included in previous chapters to generate a full marketing campaign, including steps such as detecting an opportunity, generation of promotions, communication to potential customers, and analyzing and reporting sales produced by promotions. This chapter shows how to combine different elements to create powerful systems. Chapter 11, Calling AI Models, shows you how to integrate an AI model to work with text and images into your code, both from an externally hosted provider as well as using a local model. Chapter 12, Adding Tools to GenAI, discusses how to enhance AI models' abilities to interact with different tools, including the definition of MCP servers to multiply the capacities of the models by making them able to perform specific actions. Chapter 13, Building AI Agents for Business Automation, explores the ReAct architecture to create AI Agents capable of executing complex tasks by combining multiple tools. Chapter 14, Real-World AI Example, combines all the techniques and lessons from the previous chapters to generate a full example of an agent to perform a complex task, and discusses the implications in terms of security, performance, costs, and others. These are important elements for real work! Chapter 15, Automatic Testing Routines, explores the write and execute tests to verify that code is behaving as expected. The pytest framework is presented and common scenarios for working effectively with tests are discussed. xv Preface
Page
17
Chapter 16, Debugging Techniques, features different methods and tips for helping with the debugging process to ensure the quality of your software. It leverages the great introspection capabilities of Python and its out-of-the-box debugging tools for fixing problems and producing solid automated software. To get the most out of this book Before reading this book, readers need to know the basics of the Python language. We do not assume that the reader is an expert in the language. The reader needs to know how to input commands in the command line (Terminal, Bash, or equivalent). To understand the code in this book you need a text editor, which will enable you to read and edit the code. Use an IDE that supports the Python language, such as PyCharm. Download the example code files The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/ Python-Automation-Cookbook-Third-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing. Check them out! Download the color images We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781806702893. Conventions used There are a number of text conventions used throughout this book. CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "Finally, the meeting_preparation_agent.py script is the one that orchestrates everything." A block of code is set as follows: def main(base_url, to_search): checked_links = set() to_check = [base_url] max_checks = 10 • • • Preface xvi
Page
18
Any command-line input or output is written as follows: >>> result = requests.get('https://jsonplaceholder.typicode.com/posts') Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: "This is the specification of the standard output (stdout)" Get in touch Feedback from our readers is always welcome. General feedback: If you have questions about any aspect of this book or have any general feedback, please email us at customercare@packt.com and mention the book's title in the subject of your message. Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packt.com/submit-errata, click Submit Errata, and fill in the form. Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material. If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http:// authors.packt.com/. Warnings or important notes appear like this. Note Tips and tricks appear like this. Tip xvii Preface
Page
19
Share your thoughts Once you've read Python Automation Cookbook, Third Edition, we'd love to hear your thoughts! Scan the QR code below to go straight to the Amazon review page for this book and share your feedback. https://packt.link/r/1806702894 Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content. Preface xviii
Page
20
Free benefits with your book This book comes with free benefits to support your learning. Activate them now for instant access (see the "How to Unlock" section for instructions). Here's a quick overview of what you can instantly unlock with your purchase: xix Preface