天天看点

pandas 读写 xls、xlsx

import pandas as pd 

import pandas as pdw

dfw=pdw.DataFrame({"时间":["时间"],"地点":["地点"],"人物":["人物"],"事由":["事由"]})

wenjian=open("k.txt")   #k.txt文件是“dir *.xls *.xlsx /w >k.txt”目录文件

liebiao=wenjian.readlines() 

lines=[] 

i=0

for s in liebiao: 

    try:

        s=s.strip('\n')  #去掉回车换行符

        io=s 

        df=pd.read_excel( io,sheet_name=0,header=None) #将xls读入pandas,注意header=None

        b2=df.iloc[1,1] #表格B2

        b3=df.iloc[2,1] #表格B3

        b4=df.iloc[3,1] 

        b6=df.iloc[5,1] 

        #xieru=b2+"|"+b3+"|"+b4+"|"+b6+"\n" 

        #lines.append(xieru) 

        row1={"时间":b2,"地点":b3,"人物":b4,"事由":b6}

        dfw.loc[i]=row1  #写入第i行

        i=i+1

    except Exception as e:   #异常处理

        print(e)

writer = pdw.ExcelWriter("000营销明细.xls")  

dfw.to_excel(writer, sheet_name='明细', index=False)  #将DataFrame写入xls

writer.save()

wenjian.close() 

#wenjian2=open("结果.txt",'w') 

#wenjian2.writelines(lines)     

#wenjian2.close()