mdutils.tools package

Submodules

mdutils.tools.tools module

class mdutils.tools.tools.Header[source]

Bases: object

Contain the main methods to define Headers on a Markdown file.

Features available:

  • Create Markdown Titles: atx and setext formats are available.
  • Create Header Hanchors.
  • Auto generate a table of contents.
  • Create Tables.
  • Bold, italics, inline_code text converters.
  • Align text to center.
  • Add color to text.
static atx_level_1(title)[source]

Return a atx level 1 header.

Parameters:title (str) – text title.
Returns:a header title of form: '\n#' + title + '\n'
Return type:str
static atx_level_2(title)[source]

Return a atx level 2 header.

Parameters:title (str) – text title.
Returns:a header title of form: '\n##' + title + '\n'
Return type:str
static atx_level_3(title)[source]

Return a atx level 3 header.

Parameters:title (str) – text title.
Returns:a header title of form: '\n###' + title + '\n'
Return type:str
static atx_level_4(title)[source]

Return a atx level 4 header.

Parameters:title (str) – text title.
Returns:a header title of form: '\n####' + title + '\n'
Return type:str
static atx_level_5(title)[source]

Return a atx level 5 header.

Parameters:title (str) – text title.
Returns:a header title of form: '\n#####' + title + '\n'
Return type:str
static atx_level_6(title)[source]

Return a atx level 6 header.

Parameters:title (str) – text title.
Returns:a header title of form: '\n######' + title + '\n'
Return type:str
choose_header(level, title, style='atx')[source]

This method choose the style and the header level.

Examples:
>>> from mdutils.tools.tools import Header
>>> createHeaders = Header()
>>> createHeaders.choose_header(level=1, title='New Header', style='atx')
'\n# New Header\n'
>>> createHeaders.choose_header(level=2, title='Another Header 1', style='setext')
'\nAnother Header 1\n----------------\n'
Parameters:
  • level – Header Level, For Atx-style 1 til 6. For Setext-style 1 and 2 header levels.
  • title – Header Title.
  • style – Header Style atx or setext.
Returns:

static header_anchor(text, link='')[source]

Creates an internal link of a defined Header level 1 or level 2 in the markdown file.

Giving a text string an text link you can create an internal link of already existing header. If the link string does not contain ‘#’, it will creates an automatic link of the type #title-1.

Parameters:
  • text (str) – it is the text that will be displayed.
  • link (str) – it is the internal link.
Returns:

'[text](#link)'

Return type:

string

Example: [Title 1](#title-1)

static setext_level_1(title)[source]

Return a setext level 1 header.

Parameters:title (str) – text title.
Returns:a header titlte of form: '\n' + title +'\n==========\n'.
Return type:str
static setext_level_2(title)[source]

Return a setext level 1 header.

Parameters:title (str) – text title.
Returns:a header titlte of form: '\n' + title +'\n------------\n'.
Return type:str
class mdutils.tools.tools.Table[source]

Bases: object

create_table(columns, rows, text, text_align='center')[source]

This method takes a list of strings and creates a table.

Using arguments columns and rows allows to create a table of n columns and m rows. The columns * rows operations has to correspond to the number of elements of text list argument.
Parameters:
  • columns (int) – number of columns of the table.
  • rows (int) – number of rows of the table.
  • text (list of str) – a list of strings.
  • text_align (str) – text align argument. Values available are: 'right', 'center', and 'left'.
Returns:

a markdown table.

Return type:

str

Example:
>>> from mdutils.tools.tools import Table
>>> text_list = ['List of Items', 'Description', 'Result', 'Item 1', 'Description of item 1', '10', 'Item 2', 'Description of item 2', '0']
>>> table = Table().create_table(columns=3, rows=3, text=text_list, text_align='center')
>>> print(repr(table))
'\n|List of Items|Description|Result|\n| :---: | :---: | :---: |\n|Item 1|Description of item 1|10|\n|Item 2|Description of item 2|0|\n'
Table result on Markdown
List of Items Description Results
Item 1 Description of Item 1 10
Item 2 Description of Item 2 0
class mdutils.tools.tools.TableOfContents[source]

Bases: object

create_table_of_contents(array_of_title_contents, depth=1)[source]

This method can create a table of contents using an array of the different titles. The deep can be changed. Only accepts 1 or 2.

Parameters:
  • array_of_title_contents (list) – a string list with the different headers.
  • depth (int) – allows to include Headers 1 and 2 or only Headers of level 1. Possibly values 1 or 2.
Returns:

return a string ready to be written to a Markdown file.

Return type:

str

class mdutils.tools.tools.TextUtils[source]

Bases: object

This class helps to create bold, italics and change color text.

static bold(text)[source]

Bold text converter.

Parameters:text (str) – a text string.
Returns:a string like this example: '**text**'
Return type:str
static center_text(text)[source]

Place a text string to center.

Parameters:text (str) – a text string.
Returns:a string like this exampple: '<center>text</center>'
static inline_code(text)[source]

Inline code text converter.

Parameters:text (str) – a text string.
Returns:a string like this example: '``text’``
Return type:str
static insert_code(code, language='')[source]

This method allows to insert a peace of code.

Parameters:code – code string.

:type code:str :param language: code language: python. c++, c#… :type language: str :return: markdown style. :rtype: str

static italics(text)[source]

Italics text converter.

Parameters:text (str) – a text string.
Returns:a string like this example: '_text_'
Return type:str
static text_color(text, color='black')[source]

Change text color.

Parameters:
  • text (str) – it is the text that will be changed its color.
  • color (str) – it is the text color: 'orange', 'blue', 'red'… or a RGB color such as '#ffce00'.
Returns:

a string like this one: '<font color='color'> 'text' </font>'

Return type:

str

Using this method can be created an external link of a file or a web page.

Parameters:
  • text (str) – Text to be displayed.
  • link (str) – External Link.
Returns:

return a string like this: '[Text to be shown](https://write.link.com)'

Return type:

str

text_format(text, bold_italics_code='', color='black', align='')[source]

Text format helps to write multiple text format such as bold, italics and color.

Parameters:
  • text (str) – it is a string in which will be added the mew format
  • bold_italics_code (str) – using ‘b’: bold, ‘i’: _italics_ and ‘c’: inline_code.
  • color (str) – Can change text color. For example: ‘red’, ‘green, ‘orange’…
  • align (str) – Using this parameter you can align text.
Returns:

return a string with the new text format.

Return type:

str

Example:
>>> from mdutils.tools.tools import TextUtils
>>> textUtils = TextUtils()
>>> textUtils.text_format(text='Some Text Here', bold_italics_code='bi', color='red', align='center')
'_**<center><font color="red"> Some Text Here </font></center>**_'

Module contents