First of all, let me talk about the problem I encountered:
The excel downloaded from the system is a pivot table. Through xlrd, openpyxl, and pandas, the data in the table below cannot be obtained, and finally xlwings succeeded.
Analysis of the reason: the
downloaded excel, unless it is manually opened and then closed, will pop up whether to save the changes (nothing has been changed, as for why this is the case, I don't know), click save to get it.
The reason why xlwings can be successful may be because it can simulate manually opening excel.
1.xlrd
import xlrd
#打开excel
excel=xlrd.open_workbook("C://Users//18210//Downloads//WWReport_20200831085747.xlsx")
#选择Sheet
table=excel.sheet_by_index(0)
# table=excel.sheet_by_name('WW Report Data')
# 获取总行数
nrows=table.nrows
print(nrows)
# 获取总列数
ncols=table.ncols
print(ncols)
# 获取指定单元格的值
getVol = table.cell_value(21,2)
print(getVol)
2.openpyxl
from openpyxl import load_workbook
#打开excel
f1='C:\\Users\\18210\\Downloads\\WWReport_20200831085747.xlsx'
wb = load_workbook(f1)
ws = wb.active
# 获取总列数
nrows=ws.max_row
ncols=ws.max_column
print(nrows)
print(ncols)
getVol = ws.cell(22,4).value
print(getVol)
3.pandas
import pandas
url='C:\\Users\\18210\\Downloads\\WWReport_20200831085747.xlsx'
sheetname = 'P&L Overview' #sheet名称
data = pandas.read_excel(url,sheet_name=sheetname)
# d = data.head() #显示前五行
lines = data.iloc[22].values # iloc索引 返回列表除标题外的第一行数据
# rows = data.loc[:,'A'].values #loc标签,返回列表第一列数据
print(lines)
4.xlwings
import xlwings as xw
#指定不显示地打开Excel,读取Excel文件
app = xw.App(visible=False, add_book=False)
path = 'C:\\Users\\18210\\Downloads\\WWReport_20200831085747.xlsx'
# wb = xw.Book(path)
wb = app.books.open(path) # 打开Excel文件
ws = wb.sheets[0]# 选择第0个表单
# 获取表单使用信息:
info = ws.used_range
nrows = info.last_cell.row
print(nrows)
ncols = info.last_cell.column
print(ncols)
# data = ['北京', '上海']
# ws.range('A19').value = data
vol = ws.range('E22').value
print(vol)
wb.save()
# 关闭工作簿
wb.close()
# 退出当前活动excel程序
app.quit()
But the total number of rows and the total number of columns obtained does not seem to be correct. I don't know the specific problem
Because the number of excel columns downloaded each time is different, xlwings and xlrd are used together
import xlwings as xw
import os
import xlrd
def read_excel(file_path):
# 指定不显示地打开Excel,读取Excel文件
#file_path= 'C:\\Users\\18210\\Downloads\\WWReport_20200831085747.xlsx'
app = xw.App(visible=False, add_book=False)
wb = app.books.open(file_path) # 打开Excel文件
wb.save()
wb.close()
app.quit()
# 打开excel
excel = xlrd.open_workbook(file_path)
table = excel.sheet_by_index(0)
# nrows = table.nrows
# print(nrows)
ncols = table.ncols
print(ncols)
# 获取指定单元格的值
getVol = table.cell_value(21, ncols - 1)
getRev = table.cell_value(25, ncols - 1)
getGP = table.cell_value(28, ncols - 1)
return getVol,getRev,getGP