其他分享
首页 > 其他分享> > xlwings读取一整个excel文件xlsx的第一sheet到pandas.DataFrame的方法

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

作者:互联网

为什么不用: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)

 

标签:xlsx,startline,sheet,xlwings,excel,range,ws,pd
来源: https://www.cnblogs.com/prefertea/p/14440678.html