Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
amaaiia
Super User
Super User

Script to create (or append new sheet) a new XLSX file in lakehouse Files directory

 

Hi.

I'm trying to create a python function to create a new XLSX file in Files directory. I want to append new sheet if the file already exists, otherwise, create new file.

 

This is tha data I provide to the function:

  • dest_path = 'abfss://'+str(workspace_id)+'@onelake.dfs.fabric.microsoft.com/'+str(lakehouse_id)+"/Files/testing_file.xlsx"
  • sheet_name = 'sheet1'
  • df (pandas df)
def df_to_excel(df, dest_path, sheet_name):
    import pandas as pd

    from openpyxl import load_workbook
    try:
        import os
        file_name=os.path.basename(dest_path)
        local_path = '/tmp/'+str(file_name)
        # Copy file from Azure Blob Storage to local file system
        import fsspec
        account_name = 'onelake'
        account_host = 'onelake.dfs.fabric.microsoft.com'
        fs = fsspec.filesystem('abfss', account_name=account_name, account_host=account_host)

        with fs.open(dest_path, 'rb') as src_file:
            with open(local_path, 'wb') as dst_file:
                shutil.copyfileobj(src_file, dst_file)

        existing_book = load_workbook(local_path)
        print("Exists")
    except FileNotFoundError:
        existing_book = None
        print("Doesn't exist")

    # If exists, add existing_book. Otherwise, new file
    with pd.ExcelWriter(local_path, engine='openpyxl',mode='a') as writer:
        if existing_book:
            writer.book = existing_book
        df_pd.to_excel(writer, sheet_name=sheet_name, index=False)

But it's not working.

I copy the file into local system because load_workbook function is not able to find the file in remote server

Any ideas?

1 ACCEPTED SOLUTION
v-prasare
Community Support
Community Support

Hi amaiia,

I noticed that there are some errors in the code apart from missing an export from shutil.

The ExcelWriter context is not correctly configured when appending sheets.

You're not writing the file back to the lakehouse after saving it locally and there is a typo in df_pd.to_excel(...) — it should be df.to_excel(...)

 

You can use this code and check-

 

def df_to_excel(df, dest_path, sheet_name):
    import pandas as pd
    import shutil
    import os
    from openpyxl import load_workbook
    from pandas import ExcelWriter
    import fsspec

 

    account_name = 'onelake'
    account_host = 'onelake.dfs.fabric.microsoft.com'
    fs = fsspec.filesystem('abfss', account_name=account_name, account_host=account_host)

 

    file_name = os.path.basename(dest_path)
    local_path = f'/tmp/{file_name}'

 

    existing_book = None
    file_exists = False

 

    try:
        with fs.open(dest_path, 'rb') as src_file:
            with open(local_path, 'wb') as dst_file:
                shutil.copyfileobj(src_file, dst_file)
        existing_book = load_workbook(local_path)
        file_exists = True
        print("File exists, appending sheet.")
    except FileNotFoundError:
        print("File does not exist, will create a new one.")

 

    # Write to the Excel file
    with pd.ExcelWriter(local_path, engine='openpyxl', mode='a' if file_exists else 'w') as writer:
        if existing_book:
            writer.book = existing_book
            writer.sheets = {ws.title: ws for ws in existing_book.worksheets}
        df.to_excel(writer, sheet_name=sheet_name, index=False)

 

    # Upload updated file back to lakehouse
    with open(local_path, 'rb') as f:
        with fs.open(dest_path, 'wb') as dest_file:
            shutil.copyfileobj(f, dest_file)
    print("File written successfully.")

Hope this helps!

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

 

View solution in original post

7 REPLIES 7
v-prasare
Community Support
Community Support

 

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

 

 

If we don’t hear back, we’ll go ahead and close this thread. For any further discussions or questions, please start a new thread in the Microsoft Fabric Community Forum  we’ll be happy to assist.

Thank you for being part of the Microsoft Fabric Community.

v-prasare
Community Support
Community Support

Hi amaiia,

I noticed that there are some errors in the code apart from missing an export from shutil.

The ExcelWriter context is not correctly configured when appending sheets.

You're not writing the file back to the lakehouse after saving it locally and there is a typo in df_pd.to_excel(...) — it should be df.to_excel(...)

 

You can use this code and check-

 

def df_to_excel(df, dest_path, sheet_name):
    import pandas as pd
    import shutil
    import os
    from openpyxl import load_workbook
    from pandas import ExcelWriter
    import fsspec

 

    account_name = 'onelake'
    account_host = 'onelake.dfs.fabric.microsoft.com'
    fs = fsspec.filesystem('abfss', account_name=account_name, account_host=account_host)

 

    file_name = os.path.basename(dest_path)
    local_path = f'/tmp/{file_name}'

 

    existing_book = None
    file_exists = False

 

    try:
        with fs.open(dest_path, 'rb') as src_file:
            with open(local_path, 'wb') as dst_file:
                shutil.copyfileobj(src_file, dst_file)
        existing_book = load_workbook(local_path)
        file_exists = True
        print("File exists, appending sheet.")
    except FileNotFoundError:
        print("File does not exist, will create a new one.")

 

    # Write to the Excel file
    with pd.ExcelWriter(local_path, engine='openpyxl', mode='a' if file_exists else 'w') as writer:
        if existing_book:
            writer.book = existing_book
            writer.sheets = {ws.title: ws for ws in existing_book.worksheets}
        df.to_excel(writer, sheet_name=sheet_name, index=False)

 

    # Upload updated file back to lakehouse
    with open(local_path, 'rb') as f:
        with fs.open(dest_path, 'wb') as dest_file:
            shutil.copyfileobj(f, dest_file)
    print("File written successfully.")

Hope this helps!

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

 

v-prasare
Community Support
Community Support

@amaaiia, As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for your issue worked? or let us know if you need any further assistance here?

 

 

 

Thanks,

Prashanth Are

MS Fabric community support

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

v-prasare
Community Support
Community Support

@amaaiia, As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for your issue worked? or let us know if you need any further assistance here?

 

 

 

Thanks,

Prashanth Are

MS Fabric community support

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

v-prasare
Community Support
Community Support

@amaaiia, As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided for your issue worked? or let us know if you need any further assistance here?

 

 

 

Thanks,

Prashanth Are

MS Fabric community support

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

v-prasare
Community Support
Community Support

 

Your current function has a few issues that need fixing:

  • shutil is not imported – You are using shutil.copyfileobj() but haven't imported the module.
  • df_pd is undefined – You should be using df, which is the function argument.
  • File handling in Azure OneLake (ABFS) needs a write-back operation – You are copying the file locally, modifying it, but not writing it back to OneLake.

 

Can you please fix these and try again once.

 

 

 

Thanks,

Prashanth Are

 

Not working

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.