[Python] Want a active excel sheet names in python?

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have loaded a excel into python (Google Colab), but I was wondering if there was a way of extracting the names of the excel (.xlsm) file. Please check attached image.

    import pandas as pd
    import io
    from google.colab import files
    uploaded = files.upload()
    df = pd.read_excel(io.BytesIO(uploaded['202009 Testing - September - Diamond Plod Day & Night MKY021.xlsm']),sheet_name='1 D',header=8,usecols='BE,BH',nrows=4)
    df1 = pd.read_excel(io.BytesIO(uploaded['202009 Testing - September - Diamond Plod Day & Night MKY021.xlsm']),sheet_name='1 D',header=3)

    df=df.assign(PlodDate='D5')
    df['PlodDate']=df1.iloc[0,3]
    df=df.assign(PlodShift='D6')
    df['PlodShift']=df1.iloc[1,3]
    df =df.rename({'Qty.2':'Loads','Total (L)':'Litres'},axis=1)
    df = df.reindex(columns=['PlodDate','PlodShift','Loads','Litres','DataSource'])
    df=df.assign(DataSource='Name of the Source File')
    df


    Instead of the datasource='name of the source file', I want active excel sheet name.

    Output should be: Datasource='202009 Testing - September - Diamond Plod Day & Night MKY021'

    As I have a file for every month, I just want a code that take the name of active excel sheet when I run the code.

    I tried this code but it was not working in google colab.

    import os
    os.listdir('.')


    Excel File Name: [​IMG]

    Code Image: [​IMG]

    Code in Google Colab

    Excel File Attached

    Continue reading...

Compartilhe esta Página