A comprehensive Python library for programmatically creating and manipulating Markdown files with support for headers, tables, lists, images, links, and text formatting.
80
Central document creation, file operations, and basic content writing functionality. The MdUtils class serves as the main interface for creating and managing markdown documents, providing methods for initialization, file I/O, and fundamental text operations.
Create new markdown documents with optional title, author, and styling preferences.
class MdUtils:
def __init__(self, file_name: str, title: str = "", author: str = "", title_header_style: str = "setext"):
"""
Initialize a new markdown document.
Parameters:
- file_name (str): Name of the markdown file (without .md extension)
- title (str, optional): Document title, rendered as H1 header
- author (str, optional): Document author
- title_header_style (str): Header style for title ("setext" or "atx")
"""Usage Example:
from mdutils import MdUtils
# Basic document
md = MdUtils(file_name='basic_doc')
# Document with title and author
md = MdUtils(file_name='report', title='Monthly Report', author='John Doe')
# Document with ATX-style title
md = MdUtils(file_name='guide', title='User Guide', title_header_style='atx')Create, write, and read markdown files with comprehensive file management capabilities.
class MdUtils:
def create_md_file(self) -> MarkDownFile:
"""
Create and write the markdown file to disk.
Returns:
MarkDownFile: File object for further operations
"""
def get_md_text(self) -> str:
"""
Get the complete markdown content as a string without writing to file.
Returns:
str: Complete markdown document content
"""
def read_md_file(self, file_name: str) -> str:
"""
Read an existing markdown file and append its content to the current document.
Parameters:
- file_name (str): Path to the markdown file to read
Returns:
str: Content of the read file
"""Usage Example:
from mdutils import MdUtils
md = MdUtils(file_name='example')
md.new_paragraph('Hello World!')
# Write to file
md.create_md_file() # Creates 'example.md'
# Get content as string
content = md.get_md_text()
print(content)
# Read existing file
md.read_md_file('existing_document.md')Write text content with formatting options, positioning control, and text wrapping capabilities.
class MdUtils:
def write(self, text: str = "", bold_italics_code: str = "", color: str = "black", align: str = "", marker: str = "", wrap_width: int = 0) -> str:
"""
Write formatted text to the document.
Parameters:
- text (str): Text content to write
- bold_italics_code (str): Format codes ('b' for bold, 'i' for italics, 'c' for code)
- color (str): Text color specification
- align (str): Text alignment ('left', 'center', 'right')
- marker (str): Marker for positioning text at specific location
- wrap_width (int): Text wrapping width in characters (0 disables wrapping)
Returns:
str: Formatted text
"""
def new_paragraph(self, text: str = "", bold_italics_code: str = "", color: str = "black", align: str = "", wrap_width: int = 0) -> str:
"""
Add a new paragraph with double line breaks.
Parameters:
- text (str): Paragraph text content
- bold_italics_code (str): Format codes ('b' for bold, 'i' for italics, 'c' for code)
- color (str): Text color specification
- align (str): Text alignment ('left', 'center', 'right')
- wrap_width (int): Text wrapping width in characters
Returns:
str: Complete document content after adding paragraph
"""
def new_line(self, text: str = "", bold_italics_code: str = "", color: str = "black", align: str = "", wrap_width: int = 0) -> str:
"""
Add a new line with single line break.
Parameters:
- text (str): Line text content
- bold_italics_code (str): Format codes ('b' for bold, 'i' for italics, 'c' for code)
- color (str): Text color specification
- align (str): Text alignment ('left', 'center', 'right')
- wrap_width (int): Text wrapping width in characters
Returns:
str: Complete document content after adding line
"""Usage Example:
from mdutils import MdUtils
md = MdUtils(file_name='content_example')
# Basic text writing
md.write('This is basic text.')
# New paragraph with formatting
md.new_paragraph('This is a **bold** paragraph.', bold_italics_code='b')
# New line with color and alignment
md.new_line('Centered red text', color='red', align='center')
# Text with wrapping
long_text = 'This is a very long line that will be wrapped at 50 characters width.'
md.new_paragraph(long_text, wrap_width=50)
# Write with marker for positioning
marker = md.create_marker('content_placeholder')
md.write('This text replaces the marker', marker=marker)Key attributes available on MdUtils instances for accessing document state and components.
class MdUtils:
file_name: str # Document filename
author: str # Document author
title: str # Document title (rendered as header)
table_of_contents: str # Table of contents content
file_data_text: str # Main document content
textUtils: TextUtils # Text formatting utilities
reference: Reference # Reference link manager
image: Image # Image managerUsage Example:
from mdutils import MdUtils
md = MdUtils(file_name='document', title='My Document', author='Author Name')
# Access document properties
print(f"File: {md.file_name}")
print(f"Author: {md.author}")
print(f"Current content length: {len(md.file_data_text)}")
# Access utility components
bold_text = md.textUtils.bold('Important text')
md.write(bold_text)
# Check table of contents
if md.table_of_contents:
print("Document has table of contents")Insert syntax-highlighted code blocks with language specification for technical documentation.
class MdUtils:
def insert_code(self, code: str, language: str = "") -> str:
"""
Insert a code block with optional syntax highlighting.
Parameters:
- code (str): Code content to insert
- language (str, optional): Programming language for syntax highlighting
Returns:
str: Formatted code block
"""Usage Example:
from mdutils import MdUtils
md = MdUtils(file_name='code_example')
# Python code block
python_code = '''
def hello_world():
print("Hello, World!")
return True
'''
md.insert_code(python_code, language='python')
# JavaScript code block
js_code = '''
function greet(name) {
console.log(`Hello, ${name}!`);
}
'''
md.insert_code(js_code, language='javascript')
# Code without language specification
md.insert_code('echo "Hello World"')Install with Tessl CLI
npx tessl i tessl/pypi-mdutilsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10