word-cloud-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Word Cloud Generator

词云生成器

Create visually appealing word clouds from text, files, or word frequency dictionaries. Customize shapes, colors, fonts, and export to multiple formats.
可从文本、文件或词频字典生成视觉美观的词云,支持自定义形状、颜色、字体,并可导出为多种格式。

Quick Start

快速开始

python
from scripts.wordcloud_gen import WordCloudGenerator
python
from scripts.wordcloud_gen import WordCloudGenerator

From text

From text

wc = WordCloudGenerator("Python is amazing. Python is powerful. Code is fun.") wc.generate().save("cloud.png")
wc = WordCloudGenerator("Python is amazing. Python is powerful. Code is fun.") wc.generate().save("cloud.png")

From file

From file

wc = WordCloudGenerator.from_file("article.txt") wc.colors("plasma").max_words(100).generate().save("cloud.png")
wc = WordCloudGenerator.from_file("article.txt") wc.colors("plasma").max_words(100).generate().save("cloud.png")

From frequency dict

From frequency dict

frequencies = {"python": 50, "code": 30, "data": 25, "analysis": 20} wc = WordCloudGenerator(frequencies=frequencies) wc.shape("circle").generate().save("cloud.png")
undefined
frequencies = {"python": 50, "code": 30, "data": 25, "analysis": 20} wc = WordCloudGenerator(frequencies=frequencies) wc.shape("circle").generate().save("cloud.png")
undefined

Features

功能特性

  • Multiple Input Sources: Raw text, text files, or word frequency dictionaries
  • Shape Options: Rectangle, circle, or custom mask images
  • Color Schemes: 20+ matplotlib colormaps or custom color lists
  • Font Selection: Use system fonts or custom font files
  • Stopword Filtering: Built-in English stopwords + custom additions
  • Export Formats: PNG, SVG
  • 多输入源:原始文本、文本文件或词频字典
  • 形状选项:矩形、圆形或自定义蒙版图片
  • 配色方案:20+种matplotlib配色映射或自定义颜色列表
  • 字体选择:可使用系统字体或自定义字体文件
  • 停用词过滤:内置英文停用词+可添加自定义停用词
  • 导出格式:PNG、SVG

API Reference

API参考

Initialization

初始化

python
undefined
python
undefined

From text string

From text string

wc = WordCloudGenerator("Your text here")
wc = WordCloudGenerator("Your text here")

From frequency dictionary

From frequency dictionary

wc = WordCloudGenerator(frequencies={"word": count, ...})
wc = WordCloudGenerator(frequencies={"word": count, ...})

From file

From file

wc = WordCloudGenerator.from_file("path/to/file.txt")
undefined
wc = WordCloudGenerator.from_file("path/to/file.txt")
undefined

Configuration Methods

配置方法

All methods return
self
for chaining.
python
undefined
所有方法均返回
self
以支持链式调用。
python
undefined

Shape options

Shape options

wc.shape("rectangle") # Default rectangle wc.shape("circle") # Circular cloud wc.shape(mask="logo.png") # Custom shape from image
wc.shape("rectangle") # Default rectangle wc.shape("circle") # Circular cloud wc.shape(mask="logo.png") # Custom shape from image

Color schemes (matplotlib colormaps)

Color schemes (matplotlib colormaps)

wc.colors("viridis") # Default wc.colors("plasma") # Purple-yellow gradient wc.colors("coolwarm") # Blue-red diverging wc.colors("Set2") # Categorical colors wc.colors(custom=["#FF0000", "#00FF00", "#0000FF"]) # Custom colors
wc.colors("viridis") # Default wc.colors("plasma") # Purple-yellow gradient wc.colors("coolwarm") # Blue-red diverging wc.colors("Set2") # Categorical colors wc.colors(custom=["#FF0000", "#00FF00", "#0000FF"]) # Custom colors

Font settings

Font settings

wc.font("/path/to/font.ttf") # Custom font file
wc.font("/path/to/font.ttf") # Custom font file

Stopwords

Stopwords

wc.stopwords(use_default=True) # Use built-in stopwords wc.stopwords(words=["custom", "words"]) # Add custom stopwords wc.stopwords(words=["the", "and"], use_default=False) # Only custom
wc.stopwords(use_default=True) # Use built-in stopwords wc.stopwords(words=["custom", "words"]) # Add custom stopwords wc.stopwords(words=["the", "and"], use_default=False) # Only custom

Word limits

Word limits

wc.max_words(200) # Maximum words to display (default: 200) wc.min_word_length(3) # Minimum word length (default: 1)
wc.max_words(200) # Maximum words to display (default: 200) wc.min_word_length(3) # Minimum word length (default: 1)

Size

Size

wc.size(800, 400) # Width x Height in pixels
undefined
wc.size(800, 400) # Width x Height in pixels
undefined

Generation and Export

生成与导出

python
undefined
python
undefined

Generate the word cloud

Generate the word cloud

wc.generate()
wc.generate()

Save to file

Save to file

wc.save("output.png") # PNG format wc.save("output.svg") # SVG format (auto-detected) wc.save("output.png", format="png") # Explicit format
wc.save("output.png") # PNG format wc.save("output.svg") # SVG format (auto-detected) wc.save("output.png", format="png") # Explicit format

Display (matplotlib)

Display (matplotlib)

wc.show()
wc.show()

Get word frequencies

Get word frequencies

freqs = wc.get_frequencies() # Returns dict of word: frequency
undefined
freqs = wc.get_frequencies() # Returns dict of word: frequency
undefined

Color Schemes

配色方案

Popular Colormaps

热门配色映射

NameDescription
viridis
Blue-green-yellow (default)
plasma
Purple-orange-yellow
inferno
Black-red-yellow
magma
Black-purple-white
cividis
Blue-yellow (colorblind-safe)
coolwarm
Blue-white-red
RdYlBu
Red-yellow-blue
Spectral
Rainbow spectrum
Set1
Bold categorical
Set2
Muted categorical
Pastel1
Soft categorical
Dark2
Dark categorical
名称描述
viridis
蓝-绿-黄渐变(默认)
plasma
紫-橙-黄渐变
inferno
黑-红-黄渐变
magma
黑-紫-白渐变
cividis
蓝-黄渐变(色盲友好)
coolwarm
蓝-白-红渐变
RdYlBu
红-黄-蓝渐变
Spectral
彩虹光谱色
Set1
鲜明分类色
Set2
柔和分类色
Pastel1
浅淡分类色
Dark2
深色分类色

Custom Colors

自定义颜色

python
undefined
python
undefined

Hex colors

Hex colors

wc.colors(custom=["#1a1a2e", "#16213e", "#0f3460", "#e94560"])
wc.colors(custom=["#1a1a2e", "#16213e", "#0f3460", "#e94560"])

Named colors

Named colors

wc.colors(custom=["navy", "royalblue", "cornflowerblue", "lightsteelblue"])
undefined
wc.colors(custom=["navy", "royalblue", "cornflowerblue", "lightsteelblue"])
undefined

Shape Masks

形状蒙版

Built-in Shapes

内置形状

python
wc.shape("rectangle")  # Default
wc.shape("circle")     # Circular
wc.shape("square")     # Square
python
wc.shape("rectangle")  # Default
wc.shape("circle")     # Circular
wc.shape("square")     # Square

Custom Mask Images

自定义蒙版图片

Use any image where white areas are filled with words:
python
undefined
可使用任意图片,其中白色区域会被词填充:
python
undefined

Use logo or shape image as mask

Use logo or shape image as mask

wc.shape(mask="company_logo.png") wc.shape(mask="heart.png") wc.shape(mask="map_outline.png")

**Mask Image Requirements:**
- White (#FFFFFF) areas will be filled with words
- Black/dark areas will be empty
- Works best with high-contrast images
- Recommended: 800x800 pixels or larger
wc.shape(mask="company_logo.png") wc.shape(mask="heart.png") wc.shape(mask="map_outline.png")

**蒙版图片要求:**
- 白色(#FFFFFF)区域会被词填充
- 黑色/深色区域为空白
- 高对比度图片效果最佳
- 推荐尺寸:800x800像素或更大

CLI Usage

CLI使用方法

bash
undefined
bash
undefined

Basic usage

Basic usage

python wordcloud_gen.py --text "Your text here" --output cloud.png
python wordcloud_gen.py --text "Your text here" --output cloud.png

From file

From file

python wordcloud_gen.py --file article.txt --output cloud.png
python wordcloud_gen.py --file article.txt --output cloud.png

With options

With options

python wordcloud_gen.py --file data.txt
--shape circle
--colors plasma
--max-words 150
--output cloud.png
python wordcloud_gen.py --file data.txt \ --shape circle \ --colors plasma \ --max-words 150 \ --output cloud.png

Custom mask

Custom mask

python wordcloud_gen.py --file speech.txt
--mask logo.png
--colors Set2
--output branded_cloud.png
python wordcloud_gen.py --file speech.txt \ --mask logo.png \ --colors Set2 \ --output branded_cloud.png

SVG output

SVG output

python wordcloud_gen.py --text "Hello World" --output cloud.svg
undefined
python wordcloud_gen.py --text "Hello World" --output cloud.svg
undefined

CLI Arguments

CLI参数

ArgumentDescriptionDefault
--text
Input text string-
--file
Input text file path-
--output
Output file path
wordcloud.png
--shape
Shape: rectangle, circle, square
rectangle
--mask
Custom mask image path-
--colors
Colormap name
viridis
--max-words
Maximum words200
--min-length
Minimum word length1
--width
Image width800
--height
Image height400
--font
Custom font file-
--no-stopwords
Disable stopword filteringFalse
--stopwords
Additional stopwords (comma-separated)-
参数描述默认值
--text
输入文本字符串-
--file
输入文本文件路径-
--output
输出文件路径
wordcloud.png
--shape
形状:rectangle、circle、square
rectangle
--mask
自定义蒙版图片路径-
--colors
配色映射名称
viridis
--max-words
最大显示词数200
--min-length
最小词长度1
--width
图片宽度800
--height
图片高度400
--font
自定义字体文件路径-
--no-stopwords
禁用停用词过滤False
--stopwords
额外停用词(逗号分隔)-

Examples

示例

Basic Word Cloud

基础词云

python
text = """
Python is a versatile programming language. Python is used for web development,
data science, machine learning, and automation. Python's simplicity makes it
perfect for beginners while its power serves experts.
"""

wc = WordCloudGenerator(text)
wc.colors("plasma").generate().save("python_cloud.png")
python
text = """
Python is a versatile programming language. Python is used for web development,
data science, machine learning, and automation. Python's simplicity makes it
perfect for beginners while its power serves experts.
"""

wc = WordCloudGenerator(text)
wc.colors("plasma").generate().save("python_cloud.png")

Frequency-Based Cloud

基于词频的词云

python
undefined
python
undefined

Word frequencies from analysis

Word frequencies from analysis

tech_terms = { "Python": 100, "JavaScript": 85, "Machine Learning": 70, "API": 65, "Database": 60, "Cloud": 55, "DevOps": 45, "Kubernetes": 40, "Docker": 38, "React": 35 }
wc = WordCloudGenerator(frequencies=tech_terms) wc.shape("circle").colors("Set2").generate().save("tech_cloud.png")
undefined
tech_terms = { "Python": 100, "JavaScript": 85, "Machine Learning": 70, "API": 65, "Database": 60, "Cloud": 55, "DevOps": 45, "Kubernetes": 40, "Docker": 38, "React": 35 }
wc = WordCloudGenerator(frequencies=tech_terms) wc.shape("circle").colors("Set2").generate().save("tech_cloud.png")
undefined

Branded Word Cloud

品牌化词云

python
undefined
python
undefined

Use company logo as mask

Use company logo as mask

wc = WordCloudGenerator.from_file("company_values.txt") wc.shape(mask="logo_white_bg.png") wc.colors(custom=["#003366", "#0066cc", "#3399ff"]) wc.font("/fonts/OpenSans-Bold.ttf") wc.generate().save("branded_cloud.png")
undefined
wc = WordCloudGenerator.from_file("company_values.txt") wc.shape(mask="logo_white_bg.png") wc.colors(custom=["#003366", "#0066cc", "#3399ff"]) wc.font("/fonts/OpenSans-Bold.ttf") wc.generate().save("branded_cloud.png")
undefined

Article Analysis

文章分析

python
undefined
python
undefined

Analyze article removing common words

Analyze article removing common words

wc = WordCloudGenerator.from_file("news_article.txt") wc.stopwords(words=["said", "according", "reported"]) # Add domain stopwords wc.min_word_length(4) wc.max_words(100) wc.colors("RdYlBu") wc.generate().save("article_themes.png")
wc = WordCloudGenerator.from_file("news_article.txt") wc.stopwords(words=["said", "according", "reported"]) # Add domain stopwords wc.min_word_length(4) wc.max_words(100) wc.colors("RdYlBu") wc.generate().save("article_themes.png")

Get top words

Get top words

top_words = wc.get_frequencies() for word, freq in list(top_words.items())[:10]: print(f"{word}: {freq}")
undefined
top_words = wc.get_frequencies() for word, freq in list(top_words.items())[:10]: print(f"{word}: {freq}")
undefined

Dependencies

依赖项

wordcloud>=1.9.0
matplotlib>=3.7.0
Pillow>=10.0.0
numpy>=1.24.0
wordcloud>=1.9.0
matplotlib>=3.7.0
Pillow>=10.0.0
numpy>=1.24.0

Limitations

限制说明

  • SVG export converts from PNG (not native vector)
  • Very large texts may be slow to process
  • Custom fonts must be TrueType (.ttf) or OpenType (.otf)
  • Mask images work best with simple, high-contrast shapes
  • SVG导出是从PNG转换而来(非原生矢量图)
  • 超大文本处理速度可能较慢
  • 自定义字体必须是TrueType(.ttf)或OpenType(.otf)格式
  • 蒙版图片在简单、高对比度形状下效果最佳 ",