Markdown Blog Generator Script in Python
About the project: This is a project for a Markdown Blog Generator in Python. This tool will be a command-line script that helps you create a structured Markdown file for a blog post, including a front matter (metadata) and a body section.
This project uses only built-in Python libraries, so you won't need to install anything extra. The script will guide you through entering the title, author, date, and a brief summary, then compile a well-formatted Markdown file ready for your blog platform.
This project provides a simple yet effective tool for anyone who wants to quickly start a new blog post file with a proper structure. You can run it and it will generate a .md file in the same directory.
Project Level: Intermediate
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# markdown_blog_generator.py
import os
import datetime
def generate_blog_post():
"""
Guides the user through creating a Markdown blog post file.
"""
print("--- Python Markdown Blog Generator ---")
print("This tool will help you create a new blog post file in Markdown format.")
print("Press Ctrl+C at any time to quit.")
try:
# Get user inputs for front matter and content
title = input("\nEnter the blog post title: ").strip()
if not title:
print("Title cannot be empty. Exiting.")
return
author = input("Enter the author's name: ").strip()
# Get the current date and format it
date = datetime.datetime.now().strftime("%Y-%m-%d")
# Optional summary/description
summary = input("Enter a short summary or description of the post: ").strip()
# Get the filename from the title, replacing spaces with hyphens
# and converting to lowercase for a clean URL-friendly filename.
filename = title.lower().replace(' ', '-')
output_filename = f"{filename}.md"
# Check if the file already exists to prevent accidental overwrites
if os.path.exists(output_filename):
overwrite = input(f"Warning: '{output_filename}' already exists. Overwrite? (yes/no): ").strip().lower()
if overwrite != 'yes':
print("File generation cancelled.")
return
# Start generating the content string
markdown_content = "---\n"
markdown_content += f"title: {title}\n"
markdown_content += f"author: {author}\n"
markdown_content += f"date: {date}\n"
markdown_content += f"description: {summary}\n"
markdown_content += "---\n\n"
# Add a placeholder for the main content
markdown_content += f"# {title}\n\n"
markdown_content += "\n\n"
markdown_content += "Your content goes here. You can use Markdown to format it.\n\n"
markdown_content += "### Example Markdown\n\n"
markdown_content += "This is a paragraph with **bold text** and *italic text*.\n\n"
markdown_content += "- Here is a list item.\n"
markdown_content += "- And another one.\n\n"
markdown_content += "```python\n"
markdown_content += "# Here is an example code block\n"
markdown_content += "print('Hello, Markdown!')\n"
markdown_content += "```\n"
# Write the content to the file
with open(output_filename, "w") as file:
file.write(markdown_content)
print(f"\nSuccessfully generated Markdown file: {output_filename}")
print("You can now open the file and start writing your blog post.")
except KeyboardInterrupt:
print("\nOperation cancelled by user. Exiting.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
generate_blog_post()
← Back to Projects