import
numpy as np
import
wordcloud
from
PIL
import
Image
import
matplotlib.pyplot as plt
from
openpyxl
import
load_workbook
import
os
path
=
'词频'
files
=
[path
+
"\\"
+
i
for
i
in
os.listdir(path)]
maskImage
=
np.array(Image.
open
(
'background.png'
))
for
file
in
files:
wb
=
load_workbook(
file
)
ws
=
wb.active
wordFreq
=
{}
for
i
in
range
(
2
,ws.max_row
+
1
):
word
=
ws[
"A"
+
str
(i)].value
freq
=
ws[
"B"
+
str
(i)].value
wordFreq[word]
=
freq
wc
=
wordcloud.WordCloud(
font_path
=
'C:/Windows/Fonts/simhei.ttf'
,
mask
=
maskImage,
max_words
=
500
,
max_font_size
=
100
)
wc.generate_from_frequencies(wordFreq)
wc.to_file(
"词云图\\{}.png"
.
format
(
file
.split(
"\\"
)[
1
][:
4
]))
plt.imshow(wc)
plt.axis(
'off'
)
plt.show()