Python library for converting Markdown and HTML to WhatsApp text formatting and vice versa. Bidirectional converter for WhatsApp message formatting with support for bold, italic, strikethrough, code blocks, and nested formatting. Perfect for WhatsApp chatbots, messaging automation, and WhatsApp Business API.
Project description
py-whatsapp-formatter: Python Library for WhatsApp Text Formatting
Convert Markdown and HTML to WhatsApp formatting in Python — A lightweight, zero-dependency library for bidirectional conversion between WhatsApp, Markdown, and HTML text formatting. Perfect for WhatsApp chatbots, messaging automation, WhatsApp Business API integrations, and any application that needs to format text for WhatsApp messages.
Key Features:
- 🔄 Bidirectional conversion (to and from WhatsApp format)
- 📝 Supports Markdown ↔ WhatsApp and HTML ↔ WhatsApp
- 🎯 Zero dependencies, fully typed
- ⚡ Fast and lightweight
- 🔗 Handles nested formatting correctly
Installation
pip install py-whatsapp-formatter
Use Cases
- WhatsApp Chatbots: Format bot responses with proper WhatsApp styling
- WhatsApp Business API: Convert rich text to WhatsApp-compatible format
- Content Management: Transform CMS content (HTML/Markdown) for WhatsApp
- Message Automation: Programmatically format WhatsApp messages
- AI Chat Integration: Convert ChatGPT/Claude markdown responses to WhatsApp format
Usage
Convert to WhatsApp Format
from whatsapp_formatter import convert_markdown_to_whatsapp, convert_html_to_whatsapp
# Convert Markdown to WhatsApp format
text = convert_markdown_to_whatsapp("**Bold** and *italic* and ~~strikethrough~~")
# Output: "*Bold* and _italic_ and ~strikethrough~"
# Convert HTML to WhatsApp format
text = convert_html_to_whatsapp("<strong>Bold</strong> <em>italic</em>")
# Output: "*Bold* _italic_"
Convert from WhatsApp Format
from whatsapp_formatter import convert_whatsapp_to_markdown, convert_whatsapp_to_html
# Convert WhatsApp to Markdown format
text = convert_whatsapp_to_markdown("*Bold* and _italic_ and ~strikethrough~")
# Output: "**Bold** and *italic* and ~~strikethrough~~"
# Convert WhatsApp to HTML format
text = convert_whatsapp_to_html("*Bold* and _italic_ and ~strikethrough~")
# Output: "<strong>Bold</strong> and <em>italic</em> and <del>strikethrough</del>"
WhatsApp Formatting Reference
| Format | Markdown | HTML | |
|---|---|---|---|
| Bold | *text* |
**text** |
<b>, <strong> |
| Italic | _text_ |
*text* |
<i>, <em> |
| Strikethrough | ~text~ |
~~text~~ |
<s>, <strike>, <del> |
| Monospace | `text` |
`text` |
<code> |
| Code block | ```text``` |
```text``` |
<pre><code> |
All conversions are bidirectional. Nested formatting is supported (e.g., *_bold and italic_*).
Advanced Usage
For more control, use the converter classes directly:
from whatsapp_formatter import (
MarkdownToWhatsAppConverter,
HTMLToWhatsAppConverter,
WhatsAppToMarkdownConverter,
WhatsAppToHTMLConverter,
)
# Custom Markdown converter with post-processing
converter = MarkdownToWhatsAppConverter()
converter.add_post_processor(lambda x: x.strip())
result = converter.convert(" **Hello** ")
# HTML converter without tag stripping
converter = HTMLToWhatsAppConverter(strip_remaining_tags=False)
result = converter.convert("<b>Bold</b> <span>kept</span>")
# WhatsApp to Markdown
converter = WhatsAppToMarkdownConverter()
result = converter.convert("*bold* _italic_ ~strike~")
# Output: "**bold** *italic* ~~strike~~"
# WhatsApp to HTML (with optional line break conversion)
converter = WhatsAppToHTMLConverter(include_line_breaks=True)
result = converter.convert("*bold*\n_italic_")
# Output: "<strong>bold</strong><br>\n<em>italic</em>"
Requirements
- Python 3.9+
- No external dependencies
License
MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_whatsapp_formatter-1.0.5.tar.gz.
File metadata
- Download URL: py_whatsapp_formatter-1.0.5.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
771d2aae604632057d1d6c64df103f6d468b66bceb34e613216697a5698b9ba6
|
|
| MD5 |
4c880cdc627ae255d3fc838ca95e5acf
|
|
| BLAKE2b-256 |
b4b030acea43b975384600f9399e7a40eeb433f1ad64efc42d8171a60214136e
|
File details
Details for the file py_whatsapp_formatter-1.0.5-py3-none-any.whl.
File metadata
- Download URL: py_whatsapp_formatter-1.0.5-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c491c2c014f1d581b0270d3c3e9672f8ea9138f8494bd025ffde89ee2c184b47
|
|
| MD5 |
e7f5bad3af4c6a30b7f7135a172ea424
|
|
| BLAKE2b-256 |
023aea8d4f8dd258cf18db6150a35f838157de82d819f3145080e0beb5d91e2d
|