获取下载的xlsx和xls文件内容

import requests
from io import *
import openpyxl
import xlrd

sess = requests.session()

def get_xls_download_cont(download_content, sheet_index = 0):
    wb = xlrd.open_workbook(file_contents = download_content)
    ws = wb.sheets()[sheet_index]
    
    return_list = []
    
    for i in range(ws.nrows):
        return_list.append(ws.row_values(i))
        
    return return_list

def get_xlsx_download_cont(download_content, sheet_index = 0):
    excel_f = BytesIO(download_content)
            
    wb = openpyxl.load_workbook(filename = excel_f)
            
    ws = wb.worksheets[sheet_index]
    
    return_list = []
    
    for row_info in ws.values:
        return_list.append(row_info)
            
    return return_list


download_res = sess.get(get_url, headers = get_header_info)
    
if file_name.endswith('.xlsx'):
    all_rows = get_xlsx_download_cont(download_res.content)
elif file_name.endswith('.xls'):
    all_rows = get_xls_download_cont(download_res.content)

 

posted on 2023-03-17 14:27  帅胡  阅读(22)  评论(0编辑  收藏  举报

导航