VisualizeText1

How to Create a Word Cloud in Excel With Python

In Microsoft 365, you can now use python to help you analyze and summarize data. And one of the cool things you can do is to create a word cloud. A word cloud allows you to visualize data, specifically words, to see which ones appear most often. It can be an effective way to identify trending topics and keywords from a series of text.

For the example below, I’m going to use a word cloud to visualize Nvidia’s most recent earnings transcript, to determine which words were most prominent on the tech company’s latest earnings call. In particular, I’m going to pull the text from the question and answer section of the earnings transcript to see what analysts and management were talking about, to get an idea of which words were used more often.

The text can be grabbed in its entirety and pasted into a single cell in Excel. To get the Word Cloud to display properly, I’ll need to create the python code. I’ll break down the full code and share it with you, which you can use in your own spreadsheet.

How do you enter python code in Excel?

First thing’s first, you’ll need to enter python code properly in Excel. This isn’t just entering a regular formula that starts with an = sign. Instead, go to the Formulas tab, and there, you’ll see an option to Insert Python:

Inserting python code in Excel.

Once you click on that, you’ll see a change to your formula bar with a green column with the letters PY in it:

Entering python code in Excel.

You can now enter python code in the formula bar, and Excel will understand what you’re trying to do. And as you can see from the message below the formula bar, you’ll also need to hit Ctrl+Enter when you’re done. This will run the code. Otherwise, just pressing enter will move down a line within the python code.

Populating the code

Next, it’s time to actually put the code into the formula bar. Here’s a brief breakdown of how the code functions.

First, for the code to work, I need to import the necessary libraries and modules, which includes wordcloud and matplotlib. I also need to ensure the data is pulling from a specified cell, which in my example will be cell A1 in Sheet1.

In order to avoid commonly used words, I’ll also use a list of predefined stopwords, which can exclude common words that wouldn’t be useful in analyzing text. I’ll also add a custom list of stopwords which I can add to exclude even more words that I might not find helpful.

The last part involves creating the word cloud and visualizing it. You can adjust the height and width and background color to your preference. Here is the full code you can use in your spreadsheet:


from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt

def visualize_text_trends():

    # Get the text from cell A1
    Dataframe = xl("Sheet!A1")
    

    # Define stopwords to exclude common articles and words
    stopwords = set(STOPWORDS)

    # Add custom stopwords
    custom_stopwords = {"will", "new", "going", "use", "thank", "operator", "question", "really", "come"} 
    
    stopwords.update(custom_stopwords)
    
    # Generate the word cloud
    wordcloud = WordCloud(stopwords=stopwords, background_color='white', width=800, height=400).generate(Dataframe)

    # Display the word cloud
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis('off')  # Hide axes
    plt.show()


# Run the function
visualize_text_trends()

You can see the custom stopwords I added and you can modify it to suit your individual analysis. Since there are a lot of questions and answers on an earnings transcript and an operator is involved, I’ve excluded the words ‘operator’ and ‘question’ along with other words that you may prefer to keep.

After committing the above code, it produces the following in cell B1 (which is where I’ve entered it):

To convert this into an actual word cloud, I’ll enter the following in cell C1:

=B1.image

The data is then converted into the following:

Unsurprisingly, AI is one of the most common words on the earnings call. Blackwell, Nvidia’s AI chips, are also referred to often. Software, model, and system also are prominent words based on this analysis.


If you like this post on How to Create a Word Cloud in Excel With Python, please give this site a like on Facebook and also be sure to check out some of the many templates that we have available for download. You can also follow me on Twitter and YouTube. Also, please consider buying me a coffee if you find my website helpful and would like to support it.

Comments are closed.