import pandas as pd
import openpyxl as op
wb1=op.Workbook()
cities=['西安市','成都市','长沙市','太原市','郑州市','北京市','上海市']
years=[i for i in range(1998,2021)]
ws1=wb1['Sheet']
for j in range(len(cities)):
ws1.cell(1,j+2).value=cities[j]
for j in range(len(years)):
ws1.cell(j+2,1).value=years[j]
for i in range(len(years)):
file='gld'+str(years[i])+'_TableToExcel.xls'
ws=pd.read_excel(file)
shi=list(ws['市'])
mean=list(ws['MEAN'])
mean7=[]
for j in range(len(cities)):
if cities[j] in shi:
indx=shi.index(cities[j])
mean7.append(mean[indx])
for j in range(len(cities)):
ws1.cell(i+2,j+2).value=mean7[j]
wb1.save('1998-2020年7市某项指标汇总.xlsx')