Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

How to download folder as zip file?

Open stefaneidelloth opened this issue 3 years ago • 1 comments

The example https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/folders/download_folder.py

does not work for me because get_by_title("Documents") fails. Furthermore, what if the folder contains sub folders?

=> Is there a way to download a whole folder as zip file?

stefaneidelloth avatar Jun 23 '22 10:06 stefaneidelloth

As of what I found online (latest info from https://docs.microsoft.com/en-us/answers/questions/448711/), Microsoft's API doesn't provide a way to download a folder directly as one zip file. Therefore, it would make no sense for this library to implement a workaround, because all files would still have to be downloaded in a recursive manner, I guess.

jneuendorf avatar Sep 13 '22 11:09 jneuendorf

In 2.5.0 version the support to download folder as a zip file has been intriduced, here is an example:

def print_progress(file):
    # type: (File) -> None
    print("File {0} has been  downloaded".format(file.serverRelativeUrl))


ctx = ClientContext(site_url).with_credentials(client_credentials)
from_folder = ctx.web.lists.get_by_title("Documents").root_folder
zip_path = os.path.join(tempfile.mkdtemp(), "download.zip")
with open(zip_path, "wb") as to_file:
    from_folder.download_folder(to_file, print_progress).execute_query()
    print("Files has been downloaded: {0}".format(zip_path))

Example: download_as_zip.py

vgrem avatar Jan 30 '24 21:01 vgrem