Survey banner
The Dataiku Community is moving to a new home! We are temporary in read only mode: LEARN MORE

write a xlsm file to a Dataiku folder

Thomas_11
Level 1
write a xlsm file to a Dataiku folder

I have an xlsm file and have loaded it from a Dataiku folder and changed a few cells using openpyxl. Now I would like to save it back to the Dataiku folder without loosing the macros. Is there a way to make the below work?

file = folder.get_download_stream(my_input_file_path)
output = BytesIO()
shutil.copyfileobj(file, output)
wb = openpyxl.load_workbook(output, read_only=False, keep_vba=True)
ws = wb[sheet_name]

ws['B2'] = 'something'
ws['X6'] = 'something else'       

wb.save('my_file.xlsm')
output.seek(0)

with folder.get_writer('my_file.xlsm') as writer:
    writer.write(output.read())
1 Reply
AlexT
Dataiker

Hi @Thomas_11 ,


That should work are you seeing a specific issue/error?

Since you are using output.seek(0) you can use read() or getvalue()  on the byteIO object.

The get_writer method would work for both remove/local folders. 
You can also use upload_stream(). 
https://knowledge.dataiku.com/latest/code/managed-folders/tutorial-managed-folders.html

with folder.get_writer("filename.xlsm") as writer:
     writer.write(output.getvalue())

 

0 Kudos