Python Automation Cookbook
Second Edition
75 Python automation ideas for web scraping, data wrangling, and processing Excel, reports, emails, and more
Jaime Buelta
BIRMINGHAM - MUMBAI
Python Automation Cookbook
Second Edition
Copyright 2020 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.
Producer: Ben Renow-Clarke
Acquisition Editor Peer Reviews: Suresh Jain
Content Development Editors: Alex Patterson, Bhavesh Amin
Technical Editor: Karan Sonawane
Project Editor: Janice Gonsalves
Copy Editor: Safis Editing
Proofreader: Safis Editing
Indexer: Priyanka Dhadke
Presentation Designer: Sandip Tadge
First published: September 2018
Second published: May 2020
Production reference: 1270520
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-80020-708-0
packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
- Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
- Learn better with Skill Plans built especially for you
- Get a free eBook or video every month
- Fully searchable for easy access to vital information
- Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at for more details.
At , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
Jaime Buelta has been a professional programmer for 20 years and a full-time Python developer for over 10. During that time, he has been exposed to many different technologies. He has developed software for different industries, including aerospace, networking, industrial systems, video game online services, and finance services. In these industries, he has worked in various areas, such as marketing, management, sales, and game design, helping companies achieve their goals. He is a strong proponent of automating everything and making computers do most of the heavy lifting so users can focus on the important stuff. He is currently living in Dublin, Ireland, and is a regular speaker at PyCon Ireland.
This book could not have happened without the support and encouragement of my amazing wife, Dana. For this second edition I've worked closely with more people in the Packt team, and their help has been invaluable. Also, great thanks to the reviewers their comments have improved this book. Finally, I'd like to thank the whole Python community. I can't overstate what a joy it is to be a developer in the Python world.
About the reviewer
Michal Jaworski has more than 10 years of professional experience in writing software using various programming languages. Micha has spent most of his career writing high-performance and highly distributed backend services for web applications. His beloved language of choice was always Python.
He has had various roles at different companies, from an ordinary full-stack developer, through software architect, to VP of engineering in a fast-paced start-up. He is currently a full-time senior software engineer at Showpad. Whenever he finds any free time, he provides Python consulting for local start-ups. Micha is also an active contributor to many open source Python projects and authored the last two editions of Python Expert Programming.
Contents
Landmarks
Preface
We are all probably spending time doing small manual tasks that don't add much value. It may be scanning through information sources in search of the small bits of relevant information, working with spreadsheets to generate the same graph over and over, or searching files one by one until we find the data we're looking for. Someprobably mostof those tasks are, in fact, automatable. There's an investment upfront, but for the tasks that get repeated over and over, we can use computers to do these kinds of menial tasks and focus our own efforts instead on what humans are good forhigh-level analysis and decision making based on the result. This book will explain how to use the Python language to automate common business tasks that can be greatly sped up if a computer is doing them.
Given the expressiveness and ease of use of Python, it's surprisingly simple to start making small programs to perform these actions and combine them into more integrated systems. Throughout the book, we will show small, easy-to-follow recipes that can be adapted to your specific needs, and we will combine them to perform more complex actions. We will perform common actions, such as detecting opportunities by scraping the web, analyzing information to generate automatic spreadsheet reports with graphs, communicating with automatically generated emails, getting notifications via text messages, and learning how to run tasks while your mind is focused on other more important stuff.
Though some Python knowledge is required, the book is written with non-programmers in mind, giving clear and instructive recipes that will further the reader's proficiency while being oriented to specific day-to-day goals.
Who this book is for
This book is for Python beginners, not necessarily developers, that want to use and expand their knowledge to automate tasks. Most of the examples in the book are aimed at marketing, sales, and other non-tech areas. The reader needs to know a little of the Python language, including its basic concepts.
What this book covers
Chapter 1, Let's Begin Our Automation Journey, presents some basic content that will be used all through the book. It describes how to install and manage third-party tools through virtual environments, how to do effective string manipulation, how to use command-line arguments, and introduces you to regular expressions and other methods of text processing.
Chapter 2
Next page