Python Automation Cookbook
eBook - ePub

Python Automation Cookbook

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

Jaime Buelta

Condividi libro
  1. 398 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Python Automation Cookbook

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

Jaime Buelta

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Python Automation Cookbook è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Python Automation Cookbook di Jaime Buelta in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Programming in Python. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781788999151
Edizione
1

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...

Indice dei contenuti