Код: Выделить всё
def getPreviewDataFromExcel(blob):
try:
# Download the file as bytes
excel_file_stream = io.BytesIO(blob.download_as_bytes())
# Read the Excel file into a DataFrame with all columns as strings
df = pd.read_excel(excel_file_stream, dtype=str, na_filter=False)
# Ensure all values in the DataFrame are strings
for col in df.columns:
df[col] = df[col].astype(str)
# Check if the DataFrame is empty
if df.empty:
raise ValueError("The uploaded Excel file is empty.")
# Return the first 25 rows as a list of dictionaries
return df.head(25).to_dict(orient="records")
except ValueError as ve:
print(f"ValueError: {ve}")
raise ve
except Exception as e:
print(f"An error occurred while processing the Excel file: {e}")
raise e
Подробнее здесь: https://stackoverflow.com/questions/793 ... me-and-str