ModuleNotFoundError: No module named 'xlrd'
相关代码: mai.py # coding:utf-8 import os import time from utils.db import Mysql from utils.excel import HandleExcel from colorama import Fore, Style class Main(object): def __init__(self, table_name, excel_path): self.table_name = table_name self.excel_path = excel_path self.my_sql = Mysql() self.handle_excel = HandleExcel(excel_path) # 一键导入学生信息 def save(self): res = self.handle_excel.get_data() self.my_sql.delete_all(self.table_name) for stu_k, stu_v in res.items(): self.my_sql.insert(self.table_name, stu_k, stu_v[0], stu_v[1], stu_v[2], stu_v[4], stu_v[3]) print('.', end='') print('导入完成') def search_stu_score(self, username): res = self.my_sql.search(self.table_name, username) if len(res) == 0: print('当前查询学生不存在') return 0 for data in res: print( f'\t学号:{data[0]}\n' f'\t姓名:{data[1]}\n' f'\t性别:{data[2]}\n' f'\t语文:{data[3]} \t数学:{data[5]} \t英语:{data[4]}\n' f'\t总成绩:{data[3]+data[4]+data[5]}' ) print('\t=================================') def total_stu_score(self): res = self.my_sql.search(self.table_name) total_score = [] for data in res: total = data[3] + data[4] + data[5] stu_score = [data[0], data[1], total] total_score.append(stu_score) total_score.sort(key=lambda x: x[2], reverse=True) for index, score in enumerate(total_score): print(f'\t第{index+1}名\t学号:{score[0]}\t姓名:{score[1]}\t总分:{score[2]}') if __name__ == '__main__': table = 'three_three' file_path = r'C:\Users\46684\PycharmProjects\excel_to_mysql\script\students.xlsx' main = Main(table, file_path) while 1: print(Fore.LIGHTBLUE_EX, '\n\t============================') print(Fore.LIGHTBLUE_EX, '\n\t欢迎使用学生管理系统') print(Fore.LIGHTBLUE_EX, '\n\t============================') print(Fore.LIGHTGREEN_EX, '\n\t1 一键导入学生成绩') print(Fore.LIGHTGREEN_EX, '\n\t2 查询学生成绩信息') print(Fore.LIGHTGREEN_EX, '\n\t3 班级总成绩排名') print(Fore.LIGHTGREEN_EX, '\n\t0 退出系统') print(Fore.LIGHTBLUE_EX, '\n\t============================') print(Style.RESET_ALL) opt = input('\n\t说明:请输入数字选择菜单') if opt == '1': os.system('cls') print(Fore.LIGHTBLUE_EX, '===============================') print(Fore.LIGHTBLUE_EX, '\n\t******一键导入学生成绩*****') print(Fore.LIGHTBLUE_EX, '===============================') print(Fore.LIGHTRED_EX, '\n\t警告:使用一键导入,原班级成绩将会被覆盖') print(Style.RESET_ALL) opt_2 = input('是否继续导入?(Y/N)') if opt_2 == 'Y': main.save() elif opt_2 == 'N': print('导入过程终端,3秒后自动返回上一层') time.sleep(3) continue else: print('输入内容有误,请重新输入') elif opt == '2': os.system('cls') print(Fore.LIGHTBLUE_EX, '\n\t===============================') print(Fore.LIGHTBLUE_EX, '\n\t******查询学生成绩******') print(Fore.LIGHTBLUE_EX, '\n\t===============================') print(Fore.LIGHTGREEN_EX, '\n\t输入back返回上一层') print(Fore.LIGHTGREEN_EX, '\n\t提示:输入学生姓名查询成绩,不输入按回车键将查询所有学生成绩') print(Fore.LIGHTBLUE_EX, '\n\t===============================') print(Style.RESET_ALL) opt_2 = input('\n\t请输入学生姓名:') main.search_stu_score(opt_2) elif opt == '3': os.system('cls') print(Fore.LIGHTBLUE_EX, '\n\t===============================') print(Fore.LIGHTBLUE_EX, '\n\t******班级总成绩排名******') print(Fore.LIGHTBLUE_EX, '\n\t===============================') print(Fore.LIGHTGREEN_EX, '\n\t输入back返回上一层') print(Fore.LIGHTGREEN_EX, '\n\t提示:输入学生姓名查询成绩,不输入按回车键将查询所有学生成绩') print(Fore.LIGHTBLUE_EX, '\n\t===============================') print(Style.RESET_ALL) main.total_stu_score()
excel.py # coding:utf-8 import xlrd class HandleExcel(object): def __init__(self, filename): self.filename = filename # 读取excel表格内容 def get_data(self): excel = xlrd.open_workbook(self.filename) book = excel.sheet_by_index(0) stu_dict = {} for row_num in range(1, book.nrows): k = int(book.row_values(row_num)[0]) stu_dict[k] = book.row_values(row_num)[1:] return stu_dict
问题描述:
我已经通过pip安装了xlrd并在excel.py中导入并使用了。可我在windows终端中运行 python mai.py时报错,错误内容如下:
C:\Users\46684>python C:\Users\46684\PycharmProjects\excel_to_mysql\main.py
Traceback (most recent call last):
File "C:\Users\46684\PycharmProjects\excel_to_mysql\main.py", line 8, in <module>
from utils.excel import HandleExcel
File "C:\Users\46684\PycharmProjects\excel_to_mysql\utils\excel.py", line 4, in <module>
import xlrd
ModuleNotFoundError: No module named 'xlrd'
请问老师如何解决这个问题?
7
收起
正在回答 回答被采纳积分+1
1回答
Python全能工程师
- 参与学习 人
- 提交作业 16233 份
- 解答问题 4470 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星