import matplotlib.pyplot as plt
import xlrd
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
workbook = xlrd.open_workbook('data.xlsx')
# 获取所有sheet
print(workbook.sheet_names()) # [u'sheet1', u'sheet']
#获取sheet
sheet=workbook.sheet_by_index(0)
# sheet的名称,行数,列数
print(sheet.name,sheet.nrows,sheet.ncols)
for row in range(1,sheet.ncols):
rows = sheet.col_values(row,1,sheet.nrows)
plt.figure(figsize=(15, 10))
plt.xlabel(u"省份")
plt.ylabel(sheet.col_values(row,0,1)[0] + "消费")
plt.bar(sheet.col_values(0,1,sheet.nrows),rows)
plt.savefig("./1/" + sheet.col_values(row,0,1)[0]+"消费",dpi=600)
plt.show()
我在运行上面这些代码的就会显示 NameError: name 'XLRDError' is not defined ,在网上找了好多方法都解决不了,求大佬帮忙来解释下原因
xlrd最新版2.0.1不支持读取xlsx格式文件,参考一下这里:https://xlrd.readthedocs.io/en/latest/ 和这里:https://zhuanlan.zhihu.com/p/336905129