python大作业图书管理系统_Python大作业实验一

实验要求

人员描述信息在“人员信息.xlsx”中

1.把每一行的姓名和电话号码从A列中分别取出,姓名显示在B,电话显示在C列,保存修改后的“人员信息.xlsx”;

2.汇总此表中所有人员的描述信息,统计出现次数最多的排名前十的词汇并把结果显示在控制台,以了解多数人员共有的性格特点。

c1ebb1b9cf6e003e403c2a6f68fd5e29.png

解决思路

1.因为需要操纵xlsx表格,那么可以使用openpyxl或者pandas即可,由于openpyxl是内置库,直接导入更加方便,pandas作为第三方库需要pip。导入openpyxl后,把完成的步骤封装在一个do_xlsx的方法中。

2.由于需要统计出现次数最多的前十个词汇,便需要用到jieba库,但是jieba常见的有三种分词模式,分词的模式不应该被固定死,应当由使用者来决定。

3.可以使用PyQt5来避免黑框框的命令行,各种情况由使用者制定,不被拘束,在PyQt5里面还可以制定各种规则,比如不能设置不合理的范围,没有输入不能继续进行。再完成所有设置后,会生成一个词云图,使得统计的词汇一目了然。

最终代码

import openpyxl

import jieba

import wordcloud

import sys

import os

from collections import Counter

from PyQt5.QtGui import QIcon

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QRadioButton, QGroupBox, QLineEdit,\ QVBoxLayout, QHBoxLayout, QLabel, QMessageBox, QSpinBox

class ExperimentOne(QWidget): def __init__(self): super().__init__() # 初始化必要属性 self.wbook = openpyxl.load_workbook(r'souce/人员信息.xlsx') self.wbook_sheet1 = self.wbook['Sheet1'] self.name_list = [] self.telephone_list = [] self.cell = 2 self.all_introduction = '' # 初始化PyQt5部件 self.groupBox_font = QGroupBox('词云字体设置', self) self.hwfs_radio = QRadioButton('华文仿宋', self) self.hwxk_radio = QRadioButton('华文行楷', self) self.hwhp_radio = QRadioButton('华文琥珀', self) self.groupBox_cutmode = QGroupBox('jieba分词模式', self) self.lcut_radio = QRadioButton('精确模式', self) self.lcut_cutall_radio = QRadioButton('全模式', self) self.cut_for_search_radio = QRadioButton('搜索引擎模式', self) self.confirm_button = QPushButton('确定', self) self.spinBox = QSpinBox(self) self.spinBox.setRange(10, 200) self.spinBox_label = QLabel('设置词云图的词数量:', self) self.height_edit = QLineEdit(self) self.width_edit = QLineEdit(self) self.height_edit.setPlaceholderText('在此处编辑高度,单位px,1500左右为佳') self.width_edit.setPlaceholderText('在此处编辑长度,单位px,2000左右为佳') self.h1_layout = QHBoxLayout(self) self.h2_layout = QHBoxLayout(self) self.h3_layout = QHBoxLayout(self) self.h4_layout = QHBoxLayout(self) self.v1_layout = QVBoxLayout(self) self.v2_layout = QVBoxLayout(self) self.all_v_layout = QVBoxLayout(self) self.font_list = [self.hwhp_radio, self.hwxk_radio, self.hwfs_radio] self.mode_list = [self.lcut_radio, self.lcut_cutall_radio, self.cut_for_search_radio] self.height_label = QLabel('编辑词云图高度', self) self.width_label = QLabel('编辑词云图长度', self) self.messageBox = QMessageBox.information(self, '提示框',\ '在做完xlsx后,会生成一个前十个出现次数最多的词云图,可以设置三点:\n1.词云图的字体格式\n2.jieba的分词模式\n3.词云图的长度和高度\n4.词云图里的词数量'\ , QMessageBox.Yes) self.setWindowTitle('实验作业一') self.setWindowIcon(QIcon(r'souce/nau.jpg')) self.resize(800, 600) self.layout_init() self.QtWidget_init() self.show() def layout_init(self): ''' 布局处理 :return: ''' self.v1_layout.addWidget(self.height_label) self.v1_layout.addWidget(self.height_edit) self.v2_layout.addWidget(self.width_label) self.v2_layout.addWidget(self.width_edit) self.h1_layout.addWidget(self.hwfs_radio) self.h1_layout.addWidget(self.hwxk_radio) self.h1_layout.addWidget(self.hwhp_radio) self.h2_layout.addWidget(self.lcut_radio) self.h2_layout.addWidget(self.lcut_cutall_radio) self.h2_layout.addWidget(self.cut_for_search_radio) self.h3_layout.addLayout(self.v1_layout) self.h3_layout.addLayout(self.v2_layout) self.h4_layout.addWidget(self.spinBox_label) self.h4_layout.addWidget(self.spinBox) self.groupBox_font.setLayout(self.h1_layout) self.groupBox_cutmode.setLayout(self.h2_layout) self.all_v_layout.addWidget(self.groupBox_font) self.all_v_layout.addStretch(1) self.all_v_layout.addWidget(self.groupBox_cutmode) self.all_v_layout.addStretch(1) self.all_v_layout.addLayout(self.h3_layout) self.all_v_layout.addStretch(1) self.all_v_layout.addLayout(self.h4_layout) self.all_v_layout.addStretch(1) self.all_v_layout.addWidget(self.confirm_button) self.setLayout(self.all_v_layout) def QtWidget_init(self): ''' 让确定键不可用 :return: ''' self.confirm_button.setEnabled(False) self.height_edit.textChanged.connect(self.check_confirm_button_cha) self.width_edit.textChanged.connect(self.check_confirm_button_cha) self.confirm_button.clicked.connect(self.do_xlsx) def check_confirm_button_cha(self): ''' 检查两个编辑行是否有输入 :return: ''' if self.width_edit.text() and self.height_edit.text(): self.confirm_button.setEnabled(True) def do_xlsx(self): ''' 用来完成xlsx的方法 :return: ''' # 从A列取出姓名,电话 for col in list(self.wbook_sheet1.columns)[0][1:]: # 取到电话 telephone = col.value[-11:] # 取到不含空白字符和以:结尾的名字 pure_name = col.value[:-11].strip() if pure_name[-1] == ':': pure_name = pure_name.replace(':', '') self.name_list.append(pure_name) self.telephone_list.append(telephone) # 将名字添加到b列 for name in self.name_list: self.wbook_sheet1[f'b{self.cell}'] = name self.cell += 1 self.cell = 2 # 将名字添加到c列 for telephone in self.telephone_list: self.wbook_sheet1[f'c{self.cell}'] = telephone self.cell += 1 self.wbook.save('修改后的人员信息.xlsx') # 按照选的单选按钮的不同,设置不同的模式或者字体 for introduction in list(self.wbook_sheet1.columns)[3][1:]: self.all_introduction += introduction.value if self.lcut_radio.isChecked(): cut_word = jieba.lcut(self.all_introduction) elif self.lcut_cutall_radio.isChecked(): cut_word = jieba.lcut(self.all_introduction, cut_all=True) else: # 这里是生成器,需要强制转为list!! cut_word = list(jieba.cut_for_search(self.all_introduction)) if self.hwfs_radio.isChecked(): font = 'STFANGSO.TTF' elif self.hwxk_radio.isChecked(): font = 'STXINGKA.TTF' else: font = 'STHUPO.TTF' count = Counter(cut_word) # 因为不同的分词模式会取到不同数量的标点符号,那么就把出现次数最多的几个数字范围放大,从而避免 most_common_20 = count.most_common(20) # current代表是当前词语的序号,排在第几 current = 1 most_common_10 = '' # print(most_common_20) for number in most_common_20: if number[0] == ',' or number[0] == '。' or number[0] == '、' or number[0] == ' ': continue if current == 11: break single_word = f'{number[0]} 排在第{current}个出现了{number[1]}次' print(single_word) current += 1 most_common_10 += single_word + '\n' # print(1) # print(cut_word) # print(2) text = ' '.join(cut_word) # print(text) # 这里一定要转换类型!!!! cloud = wordcloud.WordCloud(background_color='black', width=int(self.width_edit.text()), \ height=int(self.height_edit.text()), \ font_path=font, max_words=int(self.spinBox.text())).generate(text) cloud.to_file('词云图.jpg') QMessageBox.information(self, '提示框', f'生成的词云图已保存到{os.getcwd()}目录里\n'+most_common_10, QMessageBox.Ok) self.confirm_button.setEnabled(False)

if __name__ == '__main__': app = QApplication(sys.argv) One = ExperimentOne() sys.exit(app.exec_())1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

效果展示

初进界面

刚进入界面的时候回弹出一个对话框

bd424cbcd8cd7e42bce062907589a4f7.png

点击yes后,主界面出现

f32c0180ae01d293892ffa2a4edd5b42.png

可以自由编辑 1.生成的词云字体 2.jieba分词模式 3.词云图尺寸和词云数量。如果没有设置词云图的高度和长度,则确定按钮不能被点击。如果没有设置词云字体和jieba分词模式,则会默认选择第三项。

编辑完成

37cfea6503fec6ec060a1127ea81dc9c.png

编辑完成后会弹出一个提示框,会显示出现前十的词汇以及其数量,还有保存的词云图位置。

94b124a92e12d08c720a914d0ed97107.png

最终效果

表格

e942f0b4bea2ca49d578aa92305500e7.png

词云图

67397f0c202cd5056972e991cde400b5.png

文章来源: blog.csdn.net,作者:花也没有,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_47960657/article/details/113885719

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值