根据doc格式文件生成词云图,如果含有“产品”,“技术”则突出显示,python代码
时间: 2023-02-15 15:53:59 浏览: 120
可以使用 Python 中的 wordcloud 库来生成词云图。
首先,需要安装 wordcloud 库:
```
pip install wordcloud
```
然后,你可以使用以下代码生成词云图:
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取doc文件
with open('file.doc', 'r') as f:
text = f.read()
# 生成词云图
wordcloud = WordCloud(background_color="white", max_words=2000, font_path='simsun.ttc').generate(text)
# 显示图片
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
```
其中,'file.doc' 是你的doc文件的路径。
如果你想要突出显示"产品"和"技术"这两个词,可以在生成词云图之前,将这两个词的字体设置为比较大:
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取doc文件
with open('file.doc', 'r') as f:
text = f.read()
# 生成词云图
wordcloud = WordCloud(background_color="white", max_words=2000, font_path='simsun.ttc').generate(text)
# 设置"产品"和"技术"的字体大小
wordcloud.recolor(color_func=lambda *args, **kwargs: '#FF0000', regexp=r"产品")
wordcloud.recolor(color_func=lambda *args, **kwargs: '#FF0000', regexp=r"技术")
# 显示图片
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
```
希望这些代码能帮到你!
阅读全文
相关推荐
















