[Answered ]-How to remove tags from RichTextField and calculate words saved in RichTextField Django?

1👍

You need to remove all the HTML Tags from blog.content and then count no of words. You can do this in two ways:

  1. Extract text from HTML / blog.content using BeautifulSoup.
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(blog.content, 'html.parser')
    word_count = len(soup.get_text().split())

OR

  1. Use regex for removing all the HTML Tags and then take word count.
     import re
     text = re.sub(r"]*>", " ", blog.content)
     word_count = len(text.split())
    

Leave a comment