目录
- 背景
- 使用—openpyxl(耗时89秒+输出)
- 使用—pandas(耗时44秒+输出)
- 使用—xlwings(耗时15秒+输出)
- 使用—xlrd(耗时47秒+输出)
- 总结
背景
我们在平常办公的时候,尤其是财务人员等一些经常与Excel文件打交道的小伙伴们,我们会对Excel文件进行【增】,【删】,【查】,【改】那么我们今天分享一下对于Excel文件进行读取的几种方法,首相我们需要安装一下所用到的第三方库!,今天我们统一利用一个文件20万行✖20列的数据源进行读取时间测试!
库1 | 安装 |
---|
openpyxl | pip install openpyxl |
pandas | pip install pandas |
xlwings | pip install xlwings |
xlrd | pip install xlrd |
![在这里插入图片描述](https://img-blog.csdnimg.cn/f68713da80e54ef38e3af85784de86ec.png)
使用—openpyxl(耗时89秒+输出)
python">import time
import openpyxl
t1 = time.time()
wb = openpyxl.load_workbook('数据源.xlsx')
ws = wb.active
for row in ws.iter_rows(values_only=True):
print(row)
t2 = time.time()
print("读取 耗时%.2f秒"%(t2-t1))
![在这里插入图片描述](https://img-blog.csdnimg.cn/c766ab50bac24ec0a7a3fb8528371fa1.png)
关于openpyxl更对的知识点,如:单元格修改、单元格加底、设置单元格列宽、设置单元格行高等,我都写在了这篇文章里欢迎大家订阅查看openpyxl读取Excel表格(读取、单元格修改、单元格加底色)
pandasfont_colorred44font_41">使用—pandas(耗时44秒+输出)
python">import pandas as pd
import time
def read_pd():
df = pd.read_excel("数据源.xlsx", dtype=str, keep_default_na='')
rows_data = df.values.tolist()
for r in rows_data:
print(r)
if __name__ == '__main__':
t1 = time.time()
read_pd()
t2 = time.time()
print("程序运行结束,耗时%.2f" % (t2 - t1))
![在这里插入图片描述](https://img-blog.csdnimg.cn/c48f1517ed1646d7ba48faa3763fd91c.png)
更多pandas相关内容可以订阅专栏,查看更多相关知识点,专栏地址:点我直达
![在这里插入图片描述](https://img-blog.csdnimg.cn/fd996b4edbdf46b6bdc361d546e77b48.png)
使用—xlwings(耗时15秒+输出)
python">import time
import xlwings as xw
t1 = time.time()
app = xw.App(visible=False, add_book=False)
app.display_alerts = False
app.screen_updating = False
wb = app.books.open("数据源.xlsx")
sheet = wb.sheets[0]
data = sheet.range('A1').expand().value
for r in data:
print(r)
t2 = time.time()
print("读取 耗时%.2f秒"%(t2-t1))
![在这里插入图片描述](https://img-blog.csdnimg.cn/c9e1984a60434bc8b1190b60cc2849d9.png)
关于xlwings更对的知识点,如:按行读取,按列读取,我都写在了这篇文章里欢迎大家订阅查看xlwings读取excel文件
使用—xlrd(耗时47秒+输出)
python">import xlrd
def get_excel():
with xlrd.open_workbook("JALA账单/清远-配送-6月.xlsx") as workbook:
name_sheets = workbook.sheet_names()
for index in name_sheets:
sheet_info = workbook.sheet_by_name(index)
rows = []
for row_index in range(sheet_info.nrows):
row = sheet_info.row_values(row_index)
print(row)
if __name__ == '__main__':
t1 = time.time()
apply_list = get_excel()
t2 = time.time()
print("程序运行结束,耗时%.2f"%(t2-t1))
![在这里插入图片描述](https://img-blog.csdnimg.cn/a7605a2c771e4fb6975509fc78598929.png)
总结
库 | 耗时 |
---|
openpyxl | 耗时89秒+输出 |
pandas | 耗时44秒+输出 |
xlwings | 耗时15秒+输出 |
xlrd | 耗时47秒+输出 |
希望对大家有帮助
致力于办公自动化的小小程序员一枚
都看到这了,关注+点赞+收藏=不迷路!!