Python大作業實驗一
- 實驗要求
- 解決思路
- 最終代碼
- 效果展示
實驗要求
人員描述資訊在“人員資訊.xlsx”中
1.把每一行的姓名和電話號碼從A列中分別取出,姓名顯示在B,電話顯示在C列,保存修改后的“人員資訊.xlsx”;
2.匯總此表中所有人員的描述資訊,統計出現次數最多的排名前十的詞匯并把結果顯示在控制臺,以了解多數人員共有的性格特點,

解決思路
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('<b style="color:red;">編輯詞云圖高度</b>', self)
self.width_label = QLabel('<b style="color:red;">編輯詞云圖長度</b>', 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_())
效果展示
-
初進界面
剛進入界面的時候回彈出一個對話框

點擊yes后,主界面出現

可以自由編輯 1.生成的詞云字體 2.jieba分詞模式 3.詞云圖尺寸和詞云數量,如果沒有設定詞云圖的高度和長度,則確定按鈕不能被點擊,如果沒有設定詞云字體和jieba分詞模式,則會默認選擇第三項, -
編輯完成

編輯完成后會彈出一個提示框,會顯示出現前十的詞匯以及其數量,還有保存的詞云圖位置,

-
最終效果
表格

詞云圖

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/261736.html
標籤:python
