WORDCLOUD
Overview
Generates a word cloud image from provided text data, visualizing the most frequent words in a visually engaging format. This is useful for summarizing key themes or topics from survey responses, customer feedback, or any large collection of text. The function uses the wordcloud
Python package (documentation ).
This example function is provided as-is without any representation of accuracy.
Usage
To use in Excel:
=WORDCLOUD(text, [max_words], [background_color], [colormap])
text
(list[list[str]], required): 2D list or Excel range of strings to analyze.max_words
(float, optional, default=100): Maximum number of words to display.background_color
(str, optional, default=“white”): Background color of the image.colormap
(str, optional, default=“viridis”): Matplotlib colormap for word colors.
The function returns a PNG image as a base64 string for embedding, or an error message string if input is invalid.
Examples
Example 1: Customer Feedback Word Cloud
This example summarizes the most common words from customer feedback.
In Excel:
=WORDCLOUD({"Great service","Fast delivery";"Excellent support","Great service"}, 10, "white", "viridis")
Expected output: a word cloud image as a data URL string.
Example 2: Survey Results Visualization
This example visualizes the main topics from open-ended survey responses.
In Excel:
=WORDCLOUD({"Easy to use";"User friendly interface";"Easy to use"}, 8, "black", "plasma")
Expected output: a word cloud image as a data URL string.
Python Code
options = {"insert_only":True}
import micropip
await micropip.install('wordcloud')
import matplotlib
matplotlib.use('Agg')
import base64
from io import BytesIO
from wordcloud import WordCloud
import matplotlib.pyplot as plt
def wordcloud(text, max_words=None, background_color=None, colormap=None):
"""
Generates a word cloud image from provided text data and returns a PNG image as a base64 string.
Args:
text: 2D list of strings containing the text data to analyze.
max_words: Optional; maximum number of words to display in the word cloud. Default is 100.
background_color: Optional; background color of the image. Default is "white".
colormap: Optional; matplotlib colormap for word colors. Default is "viridis".
Returns:
PNG image as a base64 string for embedding, or an error message string if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
if not isinstance(text, list) or not all(isinstance(row, list) for row in text):
return "Invalid input: text must be a 2D list of strings."
flat_text = []
for row in text:
for cell in row:
if not isinstance(cell, str):
return "Invalid input: all elements in text must be strings."
flat_text.append(cell)
joined_text = " ".join(flat_text)
if not joined_text.strip():
return "Invalid input: text is empty."
if max_words is None:
max_words = 100
else:
try:
max_words = int(max_words)
except Exception:
return "Invalid input: max_words must be a number."
if background_color is None:
background_color = "white"
if colormap is None:
colormap = "viridis"
try:
wc = WordCloud(width=800, height=400, max_words=max_words, background_color=background_color, colormap=colormap)
wc.generate(joined_text)
fig, ax = plt.subplots(figsize=(8, 4))
ax.imshow(wc, interpolation='bilinear')
ax.axis('off')
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
plt.close(fig)
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
return f"data:image/png;base64,{img_base64}"
except Exception as e:
return f"Error: {str(e)}"
Live Notebook
Edit this function in a live notebook .