python读写excel文件有很多种方法:
用xlrd和xlwt进行excel读写
用openpyxl进行excel读写
用pandas进行excel读写
本文使用xlrd读取excel文件(xls,sxls格式),使用xlwt向excel写入数据
一、xlrd和xlwt的安装
安装很简单,windos+r调出运行窗口,输入cmd,进入命令行窗口,输入以下命令。
安装xlrd: pip install xlrd
安装xlwt: pip install xlwt
xlrd的API(application programming interface)网址: https://xlrd.readthedocs.io/en/latest/api.html
在这里可以看到xlrd内的各种对象和方法
数据读取
用xlrd读取excle数据:
使用xlrd.open_wokrbook(),打开文件,得到book对象
book.sheet()[0]得到sheet对象
基于sheet对象获取各种信息,(往下还有cell对象)
import numpy as np
from matplotlib import pyplot as plt
import chinese
import xlrd
import xlwt
#导入了chinese.py来设置汉字,
chinese.set_ch()
"""读取excel文件,API见https://xlrd.readthedocs.io/en/latest/api.html&