Python Automation Cookbook
eBook - ePub

Python Automation Cookbook

Explore the world of automation using Python recipes that will enhance your skills

Jaime Buelta

Partager le livre
  1. 398 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Python Automation Cookbook

Explore the world of automation using Python recipes that will enhance your skills

Jaime Buelta

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Step-by-step instructions which take you through each program to automate monotonous tasks with Python 3.7

Key Features

  • Automate integral business processes such as report generation, email marketing, and lead generation
  • Build your first web application that scrapes data and accesses websites' APIs
  • Create graphic-rich charts, graphs, and maps using Matplotlib

Book Description

Have you been doing the same old monotonous office work over and over again? Or have you been trying to find an easy way to make your life better by automating some of your repetitive tasks? Through a tried and tested approach, understand how to automate all the boring stuff using Python.

The Python Automation Cookbook helps you develop a clear understanding of how to automate your business processes using Python, including detecting opportunities by scraping the web, analyzing information to generate automatic spreadsheets reports with graphs, and communicating with automatically generated emails.

You'll learn how to get notifications via text messages and run tasks while your mind is focused on other important activities, followed by understanding how to scan documents such as résumés. Once you've gotten familiar with the fundamentals, you'll be introduced to the world of graphs, along with studying how to produce organized charts using Matplotlib. In addition to this, you'll gain in-depth knowledge of how to generate rich graphics showing relevant information.

By the end of this book, you'll have refined your skills by attaining a sound understanding of how to identify and correct problems to produce superior and reliable systems.

What you will learn

  • Get to grips with scraping a website to detect changes
  • Search and process raw sales files to aggregate information in spreadsheets
  • Explore techniques to extract information from an Excel spreadsheet and generate exciting reports with graphs
  • Discover the techniques required to generate random, print-friendly codes to be used as single-use coupons
  • Automatically generate a marketing campaign, contacting the recipients over different channels
  • Identify and implement precise solutions

Who this book is for

The Python Automation Cookbook is for you if you are a developer or anyone who wants to automate monotonous manual tasks related to fields such as finance, sales, and HR, among others.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Python Automation Cookbook est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Python Automation Cookbook par Jaime Buelta en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming in Python. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788999151

Generating Fantastic Reports

In this chapter, we will cover the following recipes:
  • Creating a simple report in plain text
  • Using templates for reports
  • Formatting text in Markdown
  • Writing a basic Word document
  • Styling a Word document
  • Generating structure in Word documents
  • Adding pictures to Word documents
  • Writing a simple PDF document
  • Structuring a PDF
  • Aggregating PDF reports
  • Watermarking and encrypting a PDF

Introduction

In this chapter, we'll see how to write documents and perform basic operations, such as dealing with templates in different formats, such as plain text and Markdown. We'll spend the most time with common, useful formats such as Word and PDF.

Creating a simple report in plain text

The most simple report is to generate some text and store it in a file.

Getting ready

For this recipe, we will generate a brief report in text format. The data to be stored will be in a dictionary.

How to do it...

  1. Import datetime:
>>> from datetime import datetime
  1. Create the template with the report in text format:
>>> TEMPLATE = '''
Movies report
-------------

Date: {date}
Movies seen in the last 30 days: {num_movies}
Total minutes: {total_minutes}
'''
  1. Create a dictionary with the values to store. Note this is the data that's going to be presented in the report:
>>> data = {
'date': datetime.utcnow(),
'num_movies': 3,
'total_minutes': 376,
}
  1. Compose the report, adding the data to the template:
>>> report = TEMPLATE.format(**data)
  1. Create a new file with the current date and store the report:
>>> FILENAME_TMPL = "{date}_report.txt"
>>> filename = FILENAME_TMPL.format(date=data['date'].strftime('%Y-%m-%d'))
>>> filename
2018-06-26_report.txt
>>> with open(filename, 'w') as file:
... file.write(report)
  1. Check the newly created report:
$ cat 2018-06-26_report.txt

Movies report
-------------

Date: 2018-06-26 23:40:08.737671
Movies seen in the last 30 days: 3
Total minutes: 376

How it works...

Steps 2 and 3 in the How to do it
 section set up a simple template and add a dictionary with all the data to be contained in the report. Then, in step 4, those two are combined into a specific report.
In step 4, the dictionary is combined with a template. Notice that the keys on the dictionary correspond to the parameters on the template. The trick is to use the double star in the format call to decompress the dictionary, passing each of the keys as a parameter to format().
In Step 5, the resulting report, a string, is stored in a newly created file, using the with context manager. The open() function creates a new file, as stated in the opening mode, w, and keeps it open during the block, which writes the data to the file. When exiting the block, the file is properly closed.
The open modes determine how to open a file, whether it is to read or write, and whether the file is in text or binary. The w mode opens the file to write it, overwriting it if it already exists. Be careful to not to delete an existing file by mistake!
Step 6 checks that the file has been created with the proper data.

There's more...

The filename is created with today's date to minimize the probability of overwriting values. The format of the date, starting with the year and ending with the day, has been selected so the files are sorted naturally in the correct order.
The with context manager will close the file even if there's an exception. It will raise an IOError exception if there is.
Some of the common exceptions in writing could be a problem with permissions, a full hard drive, or a path problem (for instance, trying to write in a non-existent directory).
Note that a file may not be fully committed to disk until it is closed or explicitly flushed. Generally, this is not a problem when dealing with files, but something to keep in mind if trying to open a file twice, one for read and one for write.

See also

  • The Using templates for reports recipe
  • The Formatting text in Markdown recipe
  • The Aggregating PDF reports recipe

Using templates for reports

HTML is a very flexible format that can be used to present rich reports. While an HTML template can be created by treating it as just text, there are tools that allow you to add better handling of structured text. This detaches the template from the code as well, separating the generation of the data from the representation of that data.

Getting ready

The tool used in this recipe, Jinja2, reads a file that contains the template and applies the context to it. The context contains the data to be displayed.
We should start by installing the module:
$ echo "jinja2==2.20" >> requirements.txt
$ pip install -r requirements.txt
Jinja2 uses its own syntax, which is a mixture of HTML and Python. It is aimed at HTML documents so it easily performs operations such as correctly escaping special characters.
In the GitHub repository, we've included a template file called jinja_template.html with the template to use.

How to do it...

  1. Import Jinja2 Template and datetime:
>>> from jinja2 import Template
>>> from datetime import datetime
  1. Read the template from the files into memory:
>>> with open('jinja_template.html') as file:
... templat...

Table des matiĂšres