Write a word file to a sharepoint folder

Anders Registered Posts: 7 ✭✭✭


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:


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.



Best Answer

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,209 Dataiker
    edited July 17 Answer ✓

    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
        with open(temp_file.name, 'rb') as f:
            output_folder.upload_stream("test.docx", f)
        print("File uploaded to output folder.")



Setup Info
      Help me…