长虫山小木屋

没有谁会为你踏雪而来 喜欢的风景要躬亲筚路

  博客园  :: 首页  :: 新随笔  :: 联系 ::  :: 管理

为什么不用:pd.read_excel ?

因为 pd 使用 openpyxl 读取excel文件,有时候xlsx文件是由ApachIO产生的读取进去会出错,换个方式,用xlwings(基于pywin32,会调用系统自带的excel软件读取)。

传说会更快吗,没有测试速度,可以自行测试。

代码:

import xlwings as xw
from pandas import Series

def xlwings_to_df(file,startline):#文件路径,数据标题开始于第几行
    app = xw.App(visible=False,add_book=False)
    wb = app.books.open(file)
    ws = wb.sheets[0]
    x,y=ws.used_range.shape
    index=Series(ws.range((startline,1),(startline,y)).value)
    data=ws.range((startline+1,1),(x,y)).value
   wb.close()#记得关闭不然系统会启动多个excel软件直到卡死
return pd.DataFrame(data,columns=index)

 

posted on 2021-02-24 12:30  长虫山小木屋  阅读(1372)  评论(0编辑  收藏  举报