mdutils package

Submodules

mdutils.mdutils module

Module mdutils

The available features are:
  • Create Headers, Til 6 sub-levels.
  • Auto generate a table of contents.
  • Create List and sub-list.
  • Create paragraph.
  • Generate tables of different sizes.
  • Insert Links.
  • Insert Code.
  • Place text anywhere using a marker.
class mdutils.mdutils.MdUtils(file_name, title='', author='')[source]

Bases: object

This class give some basic methods that helps the creation of Markdown files while you are executing a python code.

The __init__ variables are:

  • file_name: it is the name of the Markdown file.
  • author: it is the author fo the Markdown file.
  • header: it is an instance of Header Class.
  • textUtils: it is an instance of TextUtils Class.
  • title: it is the title of the Markdown file. It is written with Setext-style.
  • table_of_contents: it is the table of contents, it can be optionally created.
  • file_data_text: contains all the file data that will be written on the markdown file.
add_new_paragraph(text='', bold_italics_code='', color='black', align='')[source]

Add a new paragraph to Markdown file. The text is saved to the global variable file_data_text.

param text:is a string containing the paragraph text. Optionally, the paragraph text is returned.
type text:str
param bold_italics_code:
 bold_italics_code: using ‘b’: bold, ‘i’: _italics_ and ‘c’: inline_code.
type bold_italics_code:
 str
param color:Can change text color. For example: ‘red’, ‘green, ‘orange’…
type color:str
param align:Using this parameter you can align text.
type align:str
return:return a string ‘

‘ + text + ‘ ‘. Not necessary to take it, if only has to be written to the file.

rtype:str
create_marker(text_marker)[source]

This will add a marker to file_data_text in order to add after some text.

Parameters:text_marker (str) – marker name.

:return return the marker. :rtype: str

create_md_file()[source]

It creates a new Markdown file. :return: return an instance of a MarkDownFile.

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

This method helps to create a table.

Parameters:
  • columns (int) – this variable defines how many columns will have the table.
  • rows (int) – this variable defines how many rows will have the table.
  • text (list) – it is a list containing all the strings which will be placed in the table.
  • text_align (str) – allows to align all the cells to the 'right', 'left' or 'center'. By default: 'center'.
  • marker (str) – using create_marker method can place the table anywhere of the markdown file.
Returns:

can return the table created as a string.

Return type:

str

new_header(level, title, style='atx')[source]
Add a new header to the Markdown file.

Examples: new_header(level=2, title='Header Title', style='atx') This will write a new level 2 Atx-style header on file_name.md:

  • \n'## Header Title\n'

new_header(level=2, title='Header Title', style='setext') This will write a new level 2 Setext-style header on file_name.md:

  • \n'Header Title\n -------------\n'
Parameters:
  • level (int) – Header level. atx style can take values 1 til 6 and setext style take values 1 and 2.
  • title (str) – Header title.
  • style (str) – Header style, can be 'atx' or 'setext'. By default 'atx' style is chosen.
new_line(text='', bold_italics_code='', color='black', align='')[source]

Add a new line to Markdown file. The text is saved to the global variable file_data_text.

param text:is a string containing the paragraph text. Optionally, the paragraph text is returned.
type text:str
param bold_italics_code:
 bold_italics_code: using ‘b’: bold, ‘i’: _italics_ and ‘c’: inline_code.
type bold_italics_code:
 str
param color:Can change text color. For example: ‘red’, ‘green, ‘orange’…
type color:str
param align:Using this parameter you can align text.
type align:str
return:return a string ‘

‘ + text + ‘ ‘. Not necessary to take it, if only has to be written to the file.

rtype:str
new_table_of_contents(table_title='Table of contents', depth=1, marker='')[source]

Table of contents can be created if Headers of ‘atx’ style have been defined.

This method allows to create a table of contents and define a title for it. Moreover, depth allows user to define if headers of level 1 and 2 or only level 1 have to be placed on the table of contents. If no marker is defined, the table of contents will be placed automatically after the file’s title.

Parameters:
  • table_title (str) – The table content’s title, by default “Table of contents”
  • depth (int) – allows to include Headers 1 and 2 or only Headers of level 1. Possible values 1 or 2.
  • marker (str) – allows to place the table of contents using a marker.
Returns:

a string with the data is returned.

Return type:

str

place_text_using_marker(text, marker)[source]

It replace a previous marker created with create_marker with a text string.

Parameters:
  • text (str) – the new string that will replace the marker.
  • marker (str) – the marker that has to be replaced.
Returns:

return a new file_data_text with the replace marker.

Return type:

str

read_md_file(file_name)[source]

Reads a Markdown file and save it to global class file_data_text.

Parameters:file_name (str) – Markdown file’s name that has to be read.
Returns:optionally returns the file data content.
Return type:str
write(text='')[source]

Module contents