← cd ../blog

Inscriptis: turn web pages into readable text, with tables

In one sentence

Inscriptis is a program that turns a web page into readable text, keeping lists and tables. It helps you get tidy text from web pages without losing their structure.

What it is and what it’s for

A web page is written in HTML, a language that describes titles, lists and tables. The browser, the program you use to surf the web, reads those descriptions and shows the page laid out on screen.

When you copy the text with a simple method, though, the structure is often lost. A method like get_text from BeautifulSoup joins the parts together: an HTML list can become one single word, such as “firstsecond”. A table turns into a string of words, with no columns.

Inscriptis avoids this problem. It converts the HTML into text that follows the layout of the page: lists stay lists, and tables stay in columns, even when they are nested, meaning one inside another. The project understands only part of HTML and of CSS, the rules that decide how things look and line up.

The project is written in Python and you can use it in three ways. You can use it inside your own program, as a library, by calling the function get_text. You can run the inscript command from the terminal, passing it a file or a web address. Finally, there is an optional web service, meaning a program that answers requests from other programs.

The result is plain text. It helps you read, compare or prepare data from saved pages, without opening a browser.

A concrete example

Exercise: convert the saved page at tests/html/wikipedia-table.html into readable text, with the table of the population of Chur lined up in columns.

If you work with saved pages, you may need this, to read or compare them without opening a browser. At the end you will have the text of the page in the terminal, with the rows “Jahr” and “Einwohner” lined up in columns.

What you will see in the terminal:

Ehre sei Gott in der Höhe!
und Friede den Menschen, die guten Willens sind.
Bevölkerung [ Bearbeiten ]
Bevölkerungsentwicklung[6]
Jahr                        1500      1860  1900    1950    1970    2000    2005    2011    2012
Einwohner                   ca. 1500  3990  11'532  19'382  31'193  32'989  32'409  36'690  37'036

The text is in German because that is the language of the test page.

Who it’s for (and who it isn’t)

Project card

What you need to try it, and what you DON’T need

You need a terminal, you need Python, version 3.11 or later. You also need the inscript command: you install it together with the project in step 1.

The project is on GitHub, at https://github.com/weblyzard/inscriptis. You download the project from there: the first step, further on, downloads it with git.

Let’s try it step by step

Step 1: download and install the project

What we do: we download the project, set it to the version we tested, and install it in a virtual environment.

Why: the project uses other pieces of software, called dependencies. A virtual environment keeps these pieces in a separate folder, without touching the rest of the computer.

Type this in the terminal, from any folder you like:

git clone https://github.com/weblyzard/inscriptis
cd inscriptis
git checkout ac9d3e88efe78635f6b4161f45832c6308c63e8a
python3 -m venv venv
source venv/bin/activate
pip install .

What you will see in the terminal:

Cloning into 'inscriptis'...
Note: switching to 'ac9d3e88efe78635f6b4161f45832c6308c63e8a'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this

What you will see: the line “Cloning into ‘inscriptis’…”, the lines of the installation and, at the end, the line that starts with “Successfully installed”.

The program itself shortens the output; here it appears in English. It says that the program is looking for and preparing the dependencies. The rest of the output is normal, even though it is long.

The block downloads the project with git clone and sets it to the tested version with git checkout. Then it creates the virtual environment with python3 -m venv venv, activates it with source venv/bin/activate, and installs the project with pip install ..

The message about “detached HEAD” is normal: the project stays fixed on the version you chose. pip is the Python tool that installs the dependencies.

How to tell it worked: at the end, a line starting with “Successfully installed” appears. It means the installation succeeded.

Step 2: convert the test page

What we do: we run the inscript command on the page tests/html/wikipedia-table.html. We are doing the exercise: the assignment asks for the text of the page with the table in columns, and this command produces it.

Why: it is a direct way to see the project at work, without writing a single line of Python.

Type this in the terminal, from the inscriptis folder:

inscript tests/html/wikipedia-table.html

What you will see in the terminal (do not type this: it is only there for comparison):

Ehre sei Gott in der Höhe!

und Friede den Menschen, die guten Willens sind.

Bevölkerung [ Bearbeiten ]

Bevölkerungsentwicklung[6]
Jahr                        1500      1860  1900    1950    1970    2000    2005    2011    2012
Einwohner                   ca. 1500  3990  11'532  19'382  31'193  32'989  32'409  36'690  37'036

What you will see: the text of the page, in German, printed in the terminal. The command does not create any file.

How to tell it worked: if the table appears in tidy columns, as in the example above, the command worked.

What happened behind the scenes

The inscript command receives the file name, opens the page and reads its HTML code. The command also accepts a web address, but in these steps we do not use it.

The project recognizes the parts of the page: titles, lines of text, lists and tables. It puts them in order, with each element in its place, even when it is inside another one.

Then it applies the style rules it supports, and lays out the text line by line. For the table, it arranges the cells in columns, so the words stay lined up from one row to the next.

This is why the result meets the assignment: the text is readable and the table stays in columns. The rows “Jahr” and “Einwohner” stay one below the other, with the values in the same positions.

Finally, the text ends up in the terminal. The project has an extra option, -o, to write it to a file, but we did not use it here.

Things to keep in mind

Where to start reading the code

Open src/inscriptis/__init__.py first: the project points to it as the starting point, and there you find the function you would use in your code.

Glossary

A note on the test

The steps were tested on Debian 12 in WSL2 (Linux), with Python 3.11.2.