A comprehensive Python library for programmatically creating and manipulating Markdown files with support for headers, tables, lists, images, links, and text formatting.
80
Comprehensive link and image support including inline and reference styles, with automatic reference management and tooltip support. MDUtils provides flexible options for incorporating links and images into markdown documents with various formatting and positioning capabilities.
Create direct inline links with optional text formatting and styling.
class MdUtils:
def new_inline_link(self, link: str, text: Optional[str] = None, bold_italics_code: str = "", align: str = "") -> str:
"""
Create inline markdown links.
Parameters:
- link (str): URL or link destination
- text (str, optional): Display text (defaults to link if not provided)
- bold_italics_code (str): Format codes ('b', 'i', 'c' and combinations)
- align (str): Text alignment ('left', 'center', 'right')
Returns:
str: Formatted inline link
"""
class Inline:
@staticmethod
def new_link(link: str, text: str = None, tooltip: str = None):
"""
Create inline links with optional tooltips.
Parameters:
- link (str): URL or link destination
- text (str, optional): Display text
- tooltip (str, optional): Tooltip text on hover
Returns:
str: Formatted inline link
"""Usage Example:
from mdutils import MdUtils
from mdutils.tools import Inline
md = MdUtils(file_name='inline_links_example')
# Basic inline links
md.new_paragraph('Visit our website at https://example.com')
# Inline link with custom text
github_link = md.new_inline_link('https://github.com/didix21/mdutils', 'MDUtils Repository')
md.new_paragraph(f'Check out the {github_link} for more information.')
# Formatted inline links
bold_link = md.new_inline_link('https://docs.python.org', 'Python Documentation', bold_italics_code='b')
italic_link = md.new_inline_link('https://markdown.org', 'Markdown Guide', bold_italics_code='i')
code_link = md.new_inline_link('https://github.com', 'GitHub', bold_italics_code='c')
md.new_paragraph(f'Resources: {bold_link}, {italic_link}, {code_link}')
# Using Inline class directly with tooltips
tooltip_link = Inline.new_link('https://example.com', 'Example Site', 'Click to visit example')
md.new_paragraph(f'Link with tooltip: {tooltip_link}')
# Links with alignment
centered_link = md.new_inline_link('https://centered-site.com', 'Centered Link', align='center')
md.write(centered_link)Create reference-style links with automatic reference management and formatting options.
class MdUtils:
def new_reference_link(self, link: str, text: str, reference_tag: Optional[str] = None, bold_italics_code: str = "", align: str = "") -> str:
"""
Create reference-style links with automatic reference collection.
Parameters:
- link (str): URL or link destination
- text (str): Display text for the link
- reference_tag (str, optional): Custom reference tag (defaults to text)
- bold_italics_code (str): Format codes for text styling
- align (str): Text alignment
Returns:
str: Reference link marker (actual link stored for document end)
"""
class Reference:
def __init__(self):
"""Initialize reference link manager."""
def new_link(self, link: str, text: str, reference_tag: str = None, tooltip: str = None) -> str:
"""
Create reference-style links with tooltip support.
Parameters:
- link (str): URL or link destination
- text (str): Display text
- reference_tag (str, optional): Custom reference identifier
- tooltip (str, optional): Tooltip text
Returns:
str: Reference link marker
"""
def get_references(self) -> dict:
"""
Get all stored reference links.
Returns:
dict: Dictionary of all reference links
"""
def get_references_as_markdown(self) -> str:
"""
Get all references formatted as markdown for document end.
Returns:
str: Formatted reference section
"""Usage Example:
from mdutils import MdUtils
md = MdUtils(file_name='reference_links_example')
# Basic reference links
md.new_header(level=1, title='Documentation Links')
# Reference link with default tag
python_ref = md.new_reference_link('https://docs.python.org', 'Python Documentation')
md.new_paragraph(f'For Python help, see {python_ref}.')
# Reference link with custom tag
github_ref = md.new_reference_link('https://github.com/didix21/mdutils', 'MDUtils', 'mdutils-repo')
md.new_paragraph(f'The source code is available at {github_ref}.')
# Formatted reference links
bold_ref = md.new_reference_link('https://important-site.com', 'Important Resource', 'important', bold_italics_code='b')
md.new_paragraph(f'Please review this {bold_ref} before proceeding.')
# Multiple references to same link with different tags
stackoverflow_ref1 = md.new_reference_link('https://stackoverflow.com', 'StackOverflow', 'so-general')
stackoverflow_ref2 = md.new_reference_link('https://stackoverflow.com', 'SO', 'so-short')
md.new_paragraph(f'Get help at {stackoverflow_ref1} or {stackoverflow_ref2}.')
# Access reference manager directly
print("Current references:", md.reference.get_references())
# References will be automatically added to document end when creating file
md.create_md_file()Add images directly inline with alt text and optional tooltips.
class MdUtils:
@staticmethod
def new_inline_image(text: str, path: str) -> str:
"""
Create inline images in markdown format.
Parameters:
- text (str): Alt text for the image
- path (str): Image path or URL
Returns:
str: Formatted inline image markdown
"""
class Image:
@staticmethod
def new_inline_image(text: str, path: str, tooltip: str = None) -> str:
"""
Create inline images with optional tooltips.
Parameters:
- text (str): Alt text for the image
- path (str): Image path or URL
- tooltip (str, optional): Tooltip text on hover
Returns:
str: Formatted inline image markdown
"""Usage Example:
from mdutils import MdUtils
from mdutils.tools import Image
md = MdUtils(file_name='inline_images_example')
# Basic inline images
md.new_header(level=1, title='Project Screenshots')
# Using MdUtils static method
logo_image = MdUtils.new_inline_image('Company Logo', './images/logo.png')
md.new_paragraph(f'Welcome to our platform: {logo_image}')
# Using Image class with tooltip
screenshot = Image.new_inline_image('Application Screenshot', './images/app-screenshot.png', 'Main application interface')
md.new_paragraph(f'Application overview: {screenshot}')
# Remote images
remote_image = MdUtils.new_inline_image('Remote Image', 'https://example.com/image.jpg')
md.new_paragraph(f'External resource: {remote_image}')
# Images in different contexts
md.new_header(level=2, title='Architecture Diagram')
diagram = Image.new_inline_image('System Architecture', './diagrams/architecture.svg', 'System component relationships')
md.write(diagram)Create reference-style images with automatic reference management.
class MdUtils:
def new_reference_image(self, text: str, path: str, reference_tag: Optional[str] = None) -> str:
"""
Create reference-style images with automatic reference collection.
Parameters:
- text (str): Alt text for the image
- path (str): Image path or URL
- reference_tag (str, optional): Custom reference tag (defaults to text)
Returns:
str: Reference image marker (actual reference stored for document end)
"""
class Image:
def new_reference_image(self, text: str, path: str, reference_tag: str = None, tooltip: str = None) -> str:
"""
Create reference-style images with tooltip support.
Parameters:
- text (str): Alt text for the image
- path (str): Image path or URL
- reference_tag (str, optional): Custom reference identifier
- tooltip (str, optional): Tooltip text
Returns:
str: Reference image marker
"""Usage Example:
from mdutils import MdUtils
md = MdUtils(file_name='reference_images_example')
md.new_header(level=1, title='Project Gallery')
# Reference images with default tags
screenshot1 = md.new_reference_image('Dashboard View', './images/dashboard.png')
screenshot2 = md.new_reference_image('Settings Panel', './images/settings.png')
md.new_paragraph(f'The main interface consists of a {screenshot1} and {screenshot2}.')
# Reference images with custom tags
flowchart = md.new_reference_image('Process Flowchart', './diagrams/process-flow.png', 'process-diagram')
workflow = md.new_reference_image('Workflow Diagram', './diagrams/workflow.png', 'workflow-diagram')
md.new_paragraph(f'Understanding the {flowchart} and {workflow} is essential.')
# Multiple references to same image
icon = md.new_reference_image('Application Icon', './images/icon.png', 'app-icon')
icon_small = md.new_reference_image('Icon', './images/icon.png', 'icon-small')
md.new_paragraph(f'The {icon} appears in various sizes, including {icon_small}.')
# References automatically added to document end
md.create_md_file()Complex usage patterns combining links, images, and formatting for rich content.
Image Links (Images as Links):
from mdutils import MdUtils
md = MdUtils(file_name='advanced_patterns')
# Create clickable images (manual approach)
md.new_header(level=2, title='Clickable Images')
# Method 1: Manual markdown combination
clickable_logo = '[](https://company.com)'
md.write(clickable_logo)
# Method 2: Combine image and link creation
image_part = MdUtils.new_inline_image('Product Screenshot', './images/product.png')
# Remove the ! to convert image to link format, then wrap in link
link_part = f'[{image_part[1:]}](https://product-demo.com)'
md.new_paragraph(f'Try the demo: {link_part}')Link Collections and Navigation:
from mdutils import MdUtils
md = MdUtils(file_name='navigation_patterns')
md.new_header(level=1, title='Quick Navigation')
# Create a navigation menu using links
nav_links = [
md.new_inline_link('#introduction', 'Introduction'),
md.new_inline_link('#features', 'Features'),
md.new_inline_link('#installation', 'Installation'),
md.new_inline_link('#usage', 'Usage'),
md.new_inline_link('#api-reference', 'API Reference')
]
md.new_paragraph(' | '.join(nav_links))
# External resource links
md.new_header(level=2, title='External Resources')
resources = {
'Documentation': 'https://docs.example.com',
'GitHub Repository': 'https://github.com/user/repo',
'Issue Tracker': 'https://github.com/user/repo/issues',
'Community Forum': 'https://forum.example.com'
}
for name, url in resources.items():
link = md.new_reference_link(url, name, name.lower().replace(' ', '-'))
md.new_line(f'- {link}')Image Galleries and Collections:
from mdutils import MdUtils
md = MdUtils(file_name='image_gallery')
md.new_header(level=1, title='Project Gallery')
# Create image gallery with descriptions
gallery_images = [
{'alt': 'Login Screen', 'path': './screenshots/login.png', 'desc': 'User authentication interface'},
{'alt': 'Dashboard', 'path': './screenshots/dashboard.png', 'desc': 'Main application dashboard'},
{'alt': 'Settings', 'path': './screenshots/settings.png', 'desc': 'Configuration options'},
{'alt': 'Reports', 'path': './screenshots/reports.png', 'desc': 'Analytics and reporting'}
]
for img in gallery_images:
md.new_header(level=3, title=img['alt'])
image_ref = md.new_reference_image(img['alt'], img['path'])
md.new_paragraph(f"{image_ref}")
md.new_paragraph(img['desc'])
# Before and after comparison
md.new_header(level=2, title='Before and After')
before_img = md.new_inline_image('Before Update', './images/before.png')
after_img = md.new_inline_image('After Update', './images/after.png')
# Create comparison table
comparison_data = [
['Before', 'After'],
[before_img, after_img]
]
md.new_table_array(data=comparison_data, text_align='center')Best practices for organizing and managing links and images in large documents.
Usage Example:
from mdutils import MdUtils
md = MdUtils(file_name='link_management')
# Consistent reference tag naming
md.new_header(level=1, title='Best Practices Guide')
# Use descriptive reference tags
api_docs = md.new_reference_link('https://api.example.com/docs', 'API Documentation', 'api-docs')
user_guide = md.new_reference_link('https://guide.example.com', 'User Guide', 'user-guide')
troubleshooting = md.new_reference_link('https://help.example.com', 'Troubleshooting', 'troubleshooting')
md.new_paragraph(f'Essential resources: {api_docs}, {user_guide}, and {troubleshooting}.')
# Group related images with consistent naming
md.new_header(level=2, title='UI Screenshots')
ui_login = md.new_reference_image('Login Interface', './ui/login.png', 'ui-login')
ui_dashboard = md.new_reference_image('Dashboard View', './ui/dashboard.png', 'ui-dashboard')
ui_settings = md.new_reference_image('Settings Panel', './ui/settings.png', 'ui-settings')
md.new_paragraph(f'Interface progression: {ui_login} → {ui_dashboard} → {ui_settings}')
# Verify references before creating file
print("Total references:", len(md.reference.get_references()))
print("Reference keys:", list(md.reference.get_references().keys()))
md.create_md_file()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