Write a word file to a sharepoint folder
Hi,
I am using the python docx library to create a word document:
doc = Document()
After filling out the document with the relevant text, I manage to write it to a "Managed folder" as follows:
doc.save('/data/dataiku/managed_folders/ProjectName/FolderID/'+fileNameOut)
This does however not work for Sharepoint folders. I tried to switch the folder-ID, but I get the following error message:
Exception: Folder is not on the local filesystem (uses fsprovider_sharepoint-online_sharepoint-online_shared-documents), cannot perform direct filesystem access. Use the read/write API instead.
I have not found the correct method from the read/write API that suites my needs.
Any advice will be appreciated.
Thanks,
Anders
Best Answer
-
Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,226 Dataiker
Hi @Anders
,
After installing python-docx here is a basic example of creating a doc and upload it to a remote managed folder, the below can be run from a notebook/recipe within DSS:import dataiku import pandas as pd, numpy as np from dataiku import pandasutils as pdu import tempfile from docx import Document # create a new Word document doc = Document() #define output folder id output_folder = dataiku.Folder("D5gWMxUp") # add some content to the document doc.add_heading('Sample Document', level=1) doc.add_paragraph('This is a sample Word document created using Python and the docx library.') # create a temporary file to save the document to with tempfile.NamedTemporaryFile(suffix='.docx', delete=False) as temp_file: # save the document to the temporary file doc.save(temp_file.name) with open(temp_file.name, 'rb') as f: output_folder.upload_stream("test.docx", f) print("File uploaded to output folder.")
,
Answers
-
Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,226 Dataiker
Hi @Anders
,
The error is because Sharepoint is remote, so you need to use rewrite APIs, e.g. upload_stream.You can find an example here where a temporary local file is created and then upload to remote managed folder which should work in your case:
https://developer.dataiku.com/latest/concepts-and-examples/managed-folders.html
Let us know if that helps! -
Hi,
Thanks for your answer. But I am sorry that I can't quite figure out how to modify this to write to a remote folder. I can make a local file (.docx), but it is not clear to me how to copy that to the remote folder.
-anders-
-
Thanks a lot, this was very helpful!
Best regards Anders