Python Automation Cookbook
eBook - ePub

Python Automation Cookbook

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

Jaime Buelta

Compartir libro
  1. 398 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Python Automation Cookbook

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

Jaime Buelta

Detalles del libro
Vista previa del libro
Índice
Citas

Información del 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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Python Automation Cookbook un PDF/ePUB en línea?
Sí, puedes acceder a Python Automation Cookbook de Jaime Buelta en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming in Python. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2018
ISBN
9781788999151
Edición
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...

Índice