SoFunction
Updated on 2025-03-04

Python uses pandas to read excel files from minio

Installation dependencies

To read Excel files from MinIO using Pandas, you need to first download the file from the MinIO bucket using the MinIO Python SDK and then read the file using Pandas.

Install the necessary libraries:

pip install pandas minio -i /simple

python 3 way

import pandas as pd
from minio import Minio
from io import BytesIO

# MinIO connection configuration# MinIO connection configurationminio_client = Minio(
    "localhost:9000",       #MinIO Server Address    access_key="admin",  # Access key    secret_key="admin",  # Key    secure=False                   # If it is a https connection, set to True)

bucket_name = "test-read"   # Bucket namefile_path = "test/"  # The path of the file in MinIO
def read_csv_from_minio(minio_client,bucket_name, file_path):
    """
     Read CSV files from MinIO and load them into pandas DataFrame
     :param bucket_name: bucket name
     :param file_path: file path in MinIO
     :return: pandas DataFrame
     """
    try:
        # Download file content from MinIO        response = minio_client.get_object(bucket_name, file_path)
        # Use BytesIO to wrap the file contents so pandas can read        # binary_data = () # Read data as bytes        df = pd.read_excel(BytesIO(), sheet_name=1)
        return df
    except Exception as e:
        print(f"Failed to read the file,error message: {e}")
        return None

# Call the function and view the data framedf = read_csv_from_minio(bucket_name, file_path)
if df is not None:
    print(())

python 2 way

import pandas as pd
from minio import Minio
from StringIO import StringIO 

# MinIO connection configuration# MinIO connection configurationminio_client = Minio(
    "localhost:9000",       #MinIO Server Address    access_key="admin",  # Access key    secret_key="admin",  # Key    secure=False                   # If it is a https connection, set to True)

bucket_name = "test-read"   # Bucket namefile_path = "test/"  # The path of the file in MinIO
def read_csv_from_minio(minio_client,bucket_name, file_path):
    """
     Read CSV files from MinIO and load them into pandas DataFrame
     :param bucket_name: bucket name
     :param file_path: file path in MinIO
     :return: pandas DataFrame
     """
    try:
        # Download file content from MinIO        response = minio_client.get_object(bucket_name, file_path)
        # Use BytesIO to wrap the file contents so pandas can read        # binary_data = () # Read data as bytes        df = pd.read_excel(StringIO(), sheet_name=1)
        return df
    except Exception as e:
        print(f"Failed to read the file,error message: {e}")
        return None

# Call the function and view the data framedf = read_csv_from_minio(bucket_name, file_path)
if df is not None:
    print(())

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.