current position:Home>A library for reading and writing markdown in Python: mdutils

A library for reading and writing markdown in Python: mdutils

2022-01-30 17:30:01 Sheep in wolf's clothing_

Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities . This article has participated in 「 Digging force Star Program 」, Win a creative gift bag , Challenge creation incentive fund .

When processing data , We may need to dynamically modify Markdown Document to show the analysis results . At this time , An easy-to-use and simple third-party library comes in handy . Hello everyone , I am a sheep in wolf's clothing , Today, I'd like to introduce to you a process Markdown Of Python library :mdutils.

PyPI Project home page :pypi.org/project/mdu…

Github Warehouse :github.com/didix21/mdu…

Official documents :mdutils.readthedocs.io/en/latest/

Write it at the front

mdutils Support ATX( The most well-known # How to write it ) and Setext(.rst The way a document is written , used Sphinx My friends should know how to ) Two styles . In addition to the common basic grammar , This library also supports text coloring 、 Center and other operations . Because I don't use these things very often ,Github Markdown Nor does it support , No discussion , Interested partners, please move to the official documents .

Directly before use pip Can be installed :

pip install mdutils
 Copy code 

establish Markdown file

from mdutils import mdutils

f = MdUtils(file_name="hello.md")
 Copy code 

file_name Of .md Suffix can be written but not written , Does not affect the output results .

MdUtils There is also an optional parameter title, Change the title to Setext The style is written at the beginning of the file .

This is a title
===============
 Copy code 

establish MdUtils The instance will not be created immediately after Markdown file , It needs to be executed manually on the last line of the code :

f.create_md_file()
 Copy code 

title

mdFile.new_header(level=1, title='Header 1')
mdFile.new_header(level=2, title='Header 2')
mdFile.new_header(level=3, title='Header 3')
mdFile.new_header(level=4, title='Header 4')
mdFile.new_header(level=5, title='Header 5')
mdFile.new_header(level=6, title='Header 6')
 Copy code 
# Header 1
## Header 2
### Header 3
#### Header 4
##### Header 5
###### Header 6
 Copy code 

Paragraph and text formatting

f.new_line("This is a **line** with line break.", wrap_width=0)
 Copy code 
This is a **line** with line break.
 Copy code 

When using this method, remember to bring wrap_width=0, Because the default value is 120, It will make your sentence wrap in strange places ( I said I didn't understand ).

form

list_of_strings = ['Items', 'Descriptions', 'Data', 'Item 0', 'Description Item 0', '0', 'Item 1', 'Description Item 1', '1', 'Item 2', 'Description Item 2', '2', 'Item 3', 'Description Item 3', '3', 'Item 4', 'Description Item 4', '4']
f.new_table(columns=3, rows=6, text=list_of_strings)
 Copy code 
|Items|Descriptions|Data|
| :---: | :---: | :---: |
|Item 0|Description Item 0|0|
|Item 1|Description Item 1|1|
|Item 2|Description Item 2|2|
|Item 3|Description Item 3|3|
|Item 4|Description Item 4|4|
 Copy code 

new_table Of text_align Parameter can make the content to the left / Right alignment , Parameter options include leftright and center, Default center.

link

f.new_line('This is an inline link: ' + f.new_inline_link(link='https://github.com/didix21/mdutils', text='mdutils'))
 Copy code 
This is an inline link: [mdutils](https://github.com/didix21/mdutils)
 Copy code 

Unlike other methods ,new_inline_link Method just returns markdown Links in format , Will not write directly to the file .

list

Unordered list

items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', ['Item 4.1', 'Item 4.2', ['Item 4.2.1', 'Item 4.2.2'], 'Item 4.3', ['Item 4.3.1']], 'Item 5']
f.new_list(items)
 Copy code 
- Item 1
- Item 2
- Item 3
- Item 4
    - Item 4.1
    - Item 4.2
        - Item 4.2.1
        - Item 4.2.2
    - Item 4.3
        - Item 4.3.1
- Item 5
 Copy code 

Ordered list

items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', ['Item 4.1', 'Item 4.2', ['Item 4.2.1', 'Item 4.2.2'], 'Item 4.3', ['Item 4.3.1']], 'Item 5']
f.new_list(items, marked_with='1')
 Copy code 
1. Item 1
2. Item 2
3. Item 3
4. Item 4
    1. Item 4.1
    2. Item 4.2
        1. Item 4.2.1
        2. Item 4.2.2
    3. Item 4.3
        1. Item 4.3.1
5. Item 5
 Copy code 

Todo list

items = ['Item 1', 'Item 2', ['1. Item 2.1', '2. Item 2.2'], 'Item 3']
f.new_checkbox_list(items)
 Copy code 
- [ ] Item 1
- [ ] Item 2
    - [ ] 1. Item 2.1
    - [ ] 2. Item 2.2
- [ ] Item 3
 Copy code 

If you want to check some of them , Add a... Before the string x

items = ['Item 1', 'Item 2', ['Item 2.1', 'x Item 2.2'], 'x Item 3']
f.new_checkbox_list(items)
 Copy code 
- [ ] Item 1
- [ ] Item 2
    - [ ] Item 2.1
    - [x] Item 2.2
- [x] Item 3
 Copy code 

I don't understand this practice very much , If I just want to show x Item 2.2 What to do ???

picture

mdFile.new_line(mdFile.new_inline_image(text='snow trees', path='https://github.com/didix21/mdutils/blob/master/doc/source/images/photo-of-snow-covered-trees.jpg'))
 Copy code 
![snow trees](https://github.com/didix21/mdutils/blob/master/doc/source/images/photo-of-snow-covered-trees.jpg)
 Copy code 

and new_inline_link equally ,new_inline_image Only return markdown Links in format , Will not write directly to the file .

Auto catalog generation

stay MdUtils.create_md_file The previous line executes :

f.new_table_of_contents(table_title='Table of Contents', depth=6 Copy code 

The generated directory will be placed at the beginning of the file . Only the first level directory is generated by default , It can be done by depth Parameters change .

summary

in general , This third-party library is commendable , There are several points to pay attention to when using :

  1. Remember to escape ]) Equal character ( As the case may be );
  2. If used online, there may be The problem of code injection , There is a risk of accessing local files ;
  3. Other points needing attention have been marked in bold , Pay attention to , Avoid trampling .

There's nothing wrong with the rest , Do you want to use this library , It's up to you .

Just as an aside , I also contributed to this library a line Code :

screencapture-github-didix21-mdutils-pull-63-2021-10-30-00_16_57.png

If there is any follow-up, we will update with you , Bye-bye ~

copyright notice
author[Sheep in wolf's clothing_],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301729575806.html

Random recommended