1、安装xlrd的whl文件:
Python读取Excel使用xlrd,到官网https://pypi.python.org/pypi/xlrd下载xlrd-1.0.0-py3-none-any.whl安装。
在C:\Program Files\Python35\Scripts目录下,运行pip命令,文件名写全路径
pip install D:\FileTest\xlrd-1.0.0-py3-none-any.whl
2、Python读取Excel文件并绘制图表
代码如下:
#coding=utf-8
#######################################################
#filename:xlrd_draw.py
#author:
#date:xxxx-xx-xx
#function:读excel文件中的数据
#######################################################
import numpy as np
import matplotlib.pyplot as plt
import xlrd
#打开一个workbook
workbook = xlrd.open_workbook('D:\\FileTest\\Barometer.xlsx')
#抓取所有sheet页的名称
worksheets = workbook.sheet_names()
print('worksheets is %s' %worksheets)
#定位到mySheet
mySheet = workbook.sheet_by_name(u'Pressure')
#get datas
pressure = mySheet.col_values(0)
#time = mySheet.col(1)
#time = [x.value for x in time]
#drop the 1st line of the data, which is the name of the data.
pressure.pop(0)
#time.pop(0)
#declare a figure object to plot
fig = plt.figure(1)
#plot pressure
plt.plot(pressure)
plt.title('Barometer')
plt.ylabel('Pa')
#plt.xticks(range(len(time)),time)
plt.show()
代码下载:
点击打开链接
在xlrd_draw.py文件所在目录中,运行命令 python xlrd_draw.py
结果如下: