The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a script to format blog post drafts

Here’s a simple Python script that formats raw blog post drafts by applying basic structural formatting like title capitalization, paragraph separation, and optional keyword highlighting. This can be useful for standardizing content before final editing or publishing.

python
import re def format_blog_post(draft, title=None, highlight_keywords=None): # Clean up leading/trailing spaces and normalize newlines cleaned = draft.strip() cleaned = re.sub(r'rn|r', 'n', cleaned) # Capitalize first letter of each paragraph paragraphs = cleaned.split('nn') formatted_paragraphs = [] for para in paragraphs: para = para.strip() if para: para = para[0].upper() + para[1:] formatted_paragraphs.append(para) # Join paragraphs with double newlines formatted_body = 'nn'.join(formatted_paragraphs) # Highlight keywords if provided if highlight_keywords: for kw in highlight_keywords: # Highlight keyword by surrounding it with ** formatted_body = re.sub(rf'b({re.escape(kw)})b', r'**1**', formatted_body, flags=re.IGNORECASE) # Format title if provided if title: formatted_title = title.strip().title() output = f"# {formatted_title}nn{formatted_body}" else: output = formatted_body return output # Example usage: if __name__ == "__main__": raw_draft = """ this is the first paragraph of the blog post. it introduces the main topic. this is the second paragraph. it contains more information and transitions the reader to the next point. here we conclude the post with a summary and maybe a call to action. """ keywords = ["blog", "information", "summary"] formatted = format_blog_post(raw_draft, title="how to format blog post drafts", highlight_keywords=keywords) print(formatted)

Features:

  • Cleans up line breaks and whitespace.

  • Capitalizes the start of each paragraph.

  • Highlights specific keywords (optional).

  • Adds a formatted title with Markdown styling.

Would you like this to include SEO metadata or integration with a CMS?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About