Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Python Automation Cookbook
Python Automation Cookbook

Python Automation Cookbook: 75 Python automation recipes for web scraping; data wrangling; and Excel, report, and email processing , Second Edition

eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Python Automation Cookbook

Automating Tasks Made Easy

To properly automate tasks, we need a way to make them execute automatically at the proper times. A task that needs to be started manually is not really fully automated.

However, in order to be able to leave them running in the background while worrying about more pressing issues, the task will need to be adequate to run in fire-and-forget mode. We should be able to monitor that it executes correctly, be sure that we capture relevant information (such as receiving notifications if something interesting arises), and know whether there have been any errors while running it.

Ensuring that a piece of software runs consistently with high reliability is actually a very big deal. It is one area that, in order to be done properly, requires specialized knowledge and staff, who typically go by the names of sysadmin, operations, or SRE (Site Reliability Engineering). Big operations, such as Amazon and Google, require huge investment in ensuring...

Preparing a task

It all starts with defining precisely the work that needs to be executed, and designing it in a way that doesn't require human intervention to run.

Some ideal characteristic points are as follows:

  1. Single, clear entry point: No confusion on how to start the task.
  2. Clear parameters: If there are any parameters, they should be as explicit as possible.
  3. No interactivity: Stopping the execution to request information from the user is not possible.
  4. The result should be stored: In order to be checked at a different time than when it runs.
  5. Clear result: When we oversee the execution of a program ourselves, we can accept more verbose results, such as unlabeled data or extra debugging information. However, for an automated task, the final result should be as concise and to the point as possible.
  6. Errors should be logged: To analyze what went wrong.

A command-line program has a lot of those characteristics already...

Setting up a cron job

Cron is an old-fashioned but reliable way of executing commands. It has been around since the 1970s in Unix, and it's an old favorite in system administration to perform maintenance tasks such as freeing up disk space, rotating log files, making backups, and other common, repetitive operations.

This recipe is Unix and Unix-like operating systems specific, so it will work in Linux and macOS. While it's possible to schedule a task in Windows, it's very different and uses Task Scheduler, which won't be described here. If you have access to a Linux server, this can be a good way of scheduling periodic tasks.

The main advantages are as follows:

  • It's present in virtually all Unix or Linux systems and configured to run automatically.
  • It's easy to use, although a little deceptive at first.
  • It's well known. Almost anyone involved with admin tasks will have a general idea of how to use it.
  • It allows...

Capturing errors and problems

An automated task's main characteristic is its fire-and-forget quality. We are not actively looking at the result, but making it run in the background.

Most of the recipes in this book deal with external information, such as web pages or other reports, so the likelihood of finding an unexpected problem when running it is high. This recipe will present an automated task that will safely store unexpected behaviors in a log file that can be checked afterward.

Getting ready

As a starting point, we'll use a task that will divide two numbers, as described in the command line.

This task is very similar to the one presented in step 5 of How it works for the Preparing a task recipe, earlier this chapter. However, instead of multiplying two numbers, we'll divide them.

How to do it...

  1. Create the task_with_error_handling_step1.py file, as follows:
    import argparse
    import sys
    def main(number, other_number...

Sending email notifications

Email has become an inescapable tool for everyday use. It's arguably the best place to send a notification if an automated task has detected something. On the other hand, email inboxes are already too full up with spam messages, so be careful.

Spam filters are also a reality. Be careful with whom to send emails to and the number of emails to be sent. An email server or address can be labeled as a spam source, and all emails may be quietly dropped by the internet.

This recipe will show you how to send a single email using an existing email account.

This approach is viable for spare emails sent to a couple of people, as a result of an automated task, but no more than that. Refer to Chapter 9, Dealing with Communication Channels, for more ideas on how to send emails, including groups.

Getting ready

For this recipe, we require a valid email account set up, which includes the following:

  • A valid email server using SMTP...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Automate integral business processes such as report generation, email marketing, and lead generation
  • Explore automated code testing and Python’s growth in data science and AI automation in three new chapters
  • Understand techniques to extract information and generate appealing graphs, and reports with Matplotlib

Description

In this updated and extended version of Python Automation Cookbook, each chapter now comprises the newest recipes and is revised to align with Python 3.8 and higher. The book includes three new chapters that focus on using Python for test automation, machine learning projects, and for working with messy data. This edition will enable you to develop a sharp understanding of the fundamentals required to automate business processes through real-world tasks, such as developing your first web scraping application, analyzing information to generate spreadsheet reports with graphs, and communicating with automatically generated emails. Once you grasp the basics, you will acquire the practical knowledge to create stunning graphs and charts using Matplotlib, generate rich graphics with relevant information, automate marketing campaigns, build machine learning projects, and execute debugging techniques. By the end of this book, you will be proficient in identifying monotonous tasks and resolving process inefficiencies to produce superior and reliable systems.

Who is this book for?

Python Automation Cookbook - Second Edition is for developers, data enthusiasts or anyone who wants to automate monotonous manual tasks related to business processes such as finance, sales, and HR, among others. Working knowledge of Python is all you need to get started with this book.

What you will learn

  • Learn data wrangling with Python and Pandas for your data science and AI projects
  • Automate tasks such as text classification, email filtering, and web scraping with Python
  • Use Matplotlib to generate a variety of stunning graphs, charts, and maps
  • Automate a range of report generation tasks, from sending SMS and email campaigns to creating templates, adding images in Word, and even encrypting PDFs
  • Master web scraping and web crawling of popular file formats and directories with tools like Beautiful Soup
  • Build cool projects such as a Telegram bot for your marketing campaign, a reader from a news RSS feed, and a machine learning model to classify emails to the correct department based on their content
  • Create fire-and-forget automation tasks by writing cron jobs, log files, and regexes with Python scripting

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 29, 2020
Length: 526 pages
Edition : 2nd
Language : English
ISBN-13 : 9781800207080
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : May 29, 2020
Length: 526 pages
Edition : 2nd
Language : English
ISBN-13 : 9781800207080
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 142.97
Modern Python Cookbook
€71.99
Python Automation Cookbook
€32.99
40 Algorithms Every Programmer Should Know
€37.99
Total 142.97 Stars icon

Table of Contents

15 Chapters
Let's Begin Our Automation Journey Chevron down icon Chevron up icon
Automating Tasks Made Easy Chevron down icon Chevron up icon
Building Your First Web Scraping Application Chevron down icon Chevron up icon
Searching and Reading Local Files Chevron down icon Chevron up icon
Generating Fantastic Reports Chevron down icon Chevron up icon
Fun with Spreadsheets Chevron down icon Chevron up icon
Cleaning and Processing Data Chevron down icon Chevron up icon
Developing Stunning Graphs Chevron down icon Chevron up icon
Dealing with Communication Channels Chevron down icon Chevron up icon
Why Not Automate Your Marketing Campaign? Chevron down icon Chevron up icon
Machine Learning for Automation Chevron down icon Chevron up icon
Automatic Testing Routines Chevron down icon Chevron up icon
Debugging Techniques Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Most Recent
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(16 Ratings)
5 star 56.3%
4 star 6.3%
3 star 18.8%
2 star 0%
1 star 18.8%
Filter icon Filter
Most Recent

Filter reviews by




Kindle Customer Aug 14, 2022
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I spent alot time to just try to understand the codes example. While the main purpose is just to think about new ideas how to automate my tasks
Amazon Verified review Amazon
Gaurav Jun 24, 2021
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I'm very disappointed that I bought this book. the author didn't include steps for windows systems which are used by more than 90 percent of the planet. Also as far as the book is concerned,m the author didn't even care to create a table of contents for easy navigation of the book. Totals waste of money and I hope Amazon gives an option to return ebooks which are below standards.
Amazon Verified review Amazon
Mukesh Sep 04, 2020
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
The explanation is not very clear. This is clearly not a book for a beginner in Python. Most of the stuff mentioned here will be easily available in a google search and the book doesn't add value to that search. The biggest drawback however was the price - extremely high.
Amazon Verified review Amazon
Michael Corley Jul 30, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
This book is very, very, basic, just from skimming it. It's more useful as a check list for little every day things that you can automate than an authoritative reference on automation in Python. If you are real beginner at Python you'll find it useful for some quick automation win's.In terms of price it's actually really expensive to pay even the 17-18 dollars for what you get. Don't bother getting a hard copy. After thumbing through it I was a little disappointed that I paid so much for it. Regardless, it's in my library now so i'll just have to live with it.
Amazon Verified review Amazon
Stephan Miller Jun 22, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book has a lot of information. First it shows you how to set up a Python environment for automation, including installing Python, setting up cron jobs to run your tasks on schedule, and adding notifications so you can get details on your jobs delivered to your email.And then it gets interesting. This book differs slightly from other cookbooks I have read that just list a bunch of small coding examples to do specific things. These recipes build on each other and can be mixed and matched to automate a process you are doing manually.The topics covered are web scraping, file processing, report generation, spreadsheet manipulation, data processing and cleaning, graph and chart generation, communication, marketing automation, machine learning, code testing and debugging. You will definitely find enough examples here to automate any process you may have and free up time.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.