1. xlrd和xlwt进行excel读写;
使用xlrd和xlwt进行excel读写(xlwt不支持xlsx)
常见报错:xlrd.biffh.XLRDError: Excel xlsx file; not supported
可以安装旧版xlrd,在cmd中运行:
pip uninstall xlrd
pip install xlrd==1.2.0
也可以用openpyxl代替xlrd打开.xlsx文件:
df=pandas.read_excel(‘data.xlsx’,engine=‘openpyxl’)
2. openpyxl进行excel读写;
只能是xlsx类型的excel
3. pandas进行excel读写;
常见问题:AttributeError: ‘DataFrame’ object has no attribute ‘ix’
属性错误:“DataFrame”对象没有属性“ix”
pandas的1.0.0版本后,已经对该函数进行了升级和重构,
可以考虑安装以前的版本:pip install pandas==0.22,
使用DataFrame的loc方法或者iloc方法进行替换,将df.ix[0].values更改为df.loc[0].values
上代码:
def get_excel_xlrd(self):
import xlrd #版本1.2.0
book = xlrd.open_workbook('date.xlsx')
sheet1 = book.sheets()[0]
nrows = sheet1.nrows
print('表格总行数', nrows)
ncols = sheet1.ncols
print('表格总列数', ncols)
row3_values = sheet1.row_values(2)
print('第3行值', row3_values)
col3_values = sheet1.col_values(2)
print('第3列值', col3_values)
cell_3_3 = sheet1.cell(2, 2).value
print('第3行第3列的单元格的值:', cell_3_3)
def post_excel_xlwt(self