xlwings读取一整个excel文件xlsx的第一sheet到pandas.DataFrame的方法

时间:2021-02-25 11:40:56   收藏:0   阅读:0

为什么不用:pd.read_excel ?

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

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

代码:

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
    return pd.DataFrame(data,columns=index)

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!