Skip to main content

Uniform way to write simple text to different file formats

Project description

mformat

The mformat package contains a number of classes providing a uniform way for a python program to write to a number of different file formats.

The primary intended use is for text output from a python program, where the programmer would like the user to be able to select the output file formats. Some users may want the text as a Microsoft Word file, others as a LibreOffice Open Document Text file, while still others might want it as Markdown. By using the uniform way of writing provided by mformat the same python code can produce output in a number of different formats.

This is intended to provide an easy and uniform way to produce information in different formats. The emphasis is on getting the same information into the different formats. This will allow you to get a correct (but perhaps rudimentary) document in several formats. If you want to produce the most estetically pleasing document in a particular format, this is not the correct library to use.

Installing mformat (base package, this package)

The base package contains support for the output formats that are supported with a minimum of dependencies. Use this if you for some reason want to avoid extra dependencies.

If you want to use it, install it using pip from https://pypi.org/project/mformat. There is no need to download anything from Bitbucket to write Python programs that use the library.

Installing base mformat on mac and Linux

pip3 install --upgrade mformat

Installing base mformat on Microsoft Windows

pip install --upgrade mformat

Installing mformat-ext (extended package)

The extended package contains support also for output formats that require some additional dependencies. Use this if you want the full selection of output formats.

If you want to use it, install it using pip from https://pypi.org/project/mformat-ext. There is no need to download anything from Bitbucket to write Python programs that use the library.

Installing extended mformat on mac and Linux

pip3 install --upgrade mformat-ext

Installing extended mformat on Microsoft Windows

pip install --upgrade mformat-ext

What it does

The main features supported in a uniform way for all supported output file formats are:

  • Factory function that takes file format and output file name as arguments
  • It opens and closes a file in the selected format, with protection against accidentically overwriting an existing file
  • The recommended way to use it is as a context manager in a with-clause, opening and closing the file
  • Headings (several levels)
  • Paragraphs
  • Nested bullet point lists
  • Nested numbered point lists
  • Mixed nested numbered point and bullet point lists
  • Tables
  • URLs in paragraphs, headings, numbered point list items and in bullet point list items

Design of program that uses mformat

It is recommended that the ouput function(s) of the a Python program using mformat should have a with-clause getting the formatting object from the factory (easiest with with create_mf(file_format=fmt, file_name=output_file_name) as). In the context of the with-clause the programmer just calls a minimum of member functions:

  • start_paragraph to start a new paragraph with some provided text content.
  • start_headingto start a new heading with some provided text content.
  • start_bullet_item to start a new bullet point list item with some provided text content, and if needed to start the bullet point list with the bullet point item.
  • start_numbered_point_item to start a new numbered point list item with some provided text content, and if needed to start the numbered point list with the number point list item.
  • add_text to add more text to an already started paragraph, heading, bullet point list item or numbered point list item.
  • add_url to add a URL (link) to an already started paragraph, heading, bullet point list item or numbered point list item.
  • start_table to start a new table with the provided first row.
  • add_table_row to add another row to an already started table.
  • write_complete_table to write a table all at once.
  • write_code_block to write some preformatted text as a code block

There are no member functions to end or close any document item. Each document item is automatically closed as another docuemnt item is started (or when closing the file at the end of the context manager scope). start_bullet_itemand start_numbered_point_item take an optional level argument, that is used to change to another nesting level.

Example programs

A number of minimal but complete example programs are provided to help the programmer new to mformat. See list of examples.

API documentation

API documentation automatically extracted from the Python code and docstrings are available here for the public API for programmers using the API and here for the protected API for programmers that want to extend the API by adding their own derived class that provide some other output format.

Even though some may like reading API documentation, the example programs probably provide a better introduction.

Version history

Version Date Python version Description
0.2 30 Jan 2026 3.12 or newer First released version

Output file formats

The following table provides information about in which version support for a format was introduced.

Format Full name of format Which package Starting at version
docx Microsoft Word mformat-ext 0.2
html Web page mformat 0.2
md Markdown mformat 0.2
odt Open Document Text mformat-ext 0.2

Test summary

  • Test result: 916 passed in 10s
  • No Flake8 warnings.
  • No mypy errors found.
  • 0.2 built and tested using python version: Python 3.14.2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mformat-0.2.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mformat-0.2-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file mformat-0.2.tar.gz.

File metadata

  • Download URL: mformat-0.2.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for mformat-0.2.tar.gz
Algorithm Hash digest
SHA256 e49dc47ff0e8588429acf04804462001a912ba022f23dc85f40f7cd9f5081064
MD5 b53c6c91a4bbc29ed90aeb2494ba2efa
BLAKE2b-256 356ecd9ad4054ca2e61a46e0fa77a011e4447fb100109f537e30f5a8579e2e0c

See more details on using hashes here.

File details

Details for the file mformat-0.2-py3-none-any.whl.

File metadata

  • Download URL: mformat-0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for mformat-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 73bbebaa917efcdd661f75cbce137cd4dfa3f13d2c9d79b1b4e8bcf82fa7c907
MD5 0f94ad42258d5ba4cdf54aff1d2a3e0c
BLAKE2b-256 edd17bdd67859ecde8573b3dd7168a6cec6d2ca5c99e7657a68437b063b62af8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page