示例#1
0
 def file(self):
     """Get file"""
     if self.is_property_available("File"):
         return self.properties["File"]
     else:
         from office365.sharepoint.file import File
         return File(
             self.context,
             ResourcePathEntity(self.context, self.resourcePath, "File"))
def download_file(context):
    path = "../ref_docs/lkpEntityLocation.xlsx"
    # path = "../ref_docs/Test.docx"
    response = File.open_binary(
        context,
        "/teams/UDPDevTeam/MasterData/dev/access/lkpEntityLocation.xlsx")
    response.raise_for_status()
    with open(path, "wb") as local_file:
        local_file.write(response.content)
 def test3_update_file_content(self):
     """Test file upload operation"""
     files = self.__class__.target_list.rootFolder.files
     self.client.load(files)
     self.client.execute_query()
     for file_upload in files:
         response = File.save_binary(self.client, file_upload.properties["ServerRelativeUrl"],
                                     self.content_placeholder)
         self.assertTrue(response.ok)
    def upload_file(self, file_path, file_name, data_type, software_version):

        upload_into_library = True

        # rn_test_1.csv
        with open(file_path, 'rb') as content_file:
            file_content = content_file.read()

        if upload_into_library:
            list_title = "Solar Test Data"
            target_folder = self.context.web.lists.get_by_title(
                list_title).root_folder

            file_extension = os.path.basename(file_path).split(".")[-1]
            # [filename][mdl]
            # [filename][xlsx][xlsm]

            split_file_name = file_name.split(".")[0]
            uploaded_file_name = split_file_name + "_" + data_type[
                0] + "." + file_extension

            file = self.upload_file_alt(target_folder, uploaded_file_name,
                                        file_content)

            new_file_url = file.properties["ServerRelativeUrl"]
            print("File url: {0}".format(new_file_url))

            #for testing you dafty remove later
            #########################

            ##################
            #data_type = "Clean data"
            #software_version = "3.1"

            update_properties = self.edit_uploaded_properties(
                file, uploaded_file_name, new_file_url, self.unit_dict,
                data_type, software_version)

            print(update_properties)
        else:
            target_url = "/Shared Documents/{0}".format(
                os.path.basename(file_path))
            File.save_binary(self.context, target_url, file_content)
 def add_template_file(self, url_of_file, template_file_type):
     """Adds a ghosted file to an existing list or document library."""
     target_file = File(self.context)
     self.add_child(target_file)
     qry = ServiceOperationQuery(self, "addTemplateFile", {
         "urlOfFile": url_of_file,
         "templateFileType": template_file_type
     }, None, None, target_file)
     self.context.add_query(qry)
     return target_file
 def add_template_file(self, url_of_file, template_file_type):
     """Adds a ghosted file to an existing list or document library."""
     file_new = File(self.context)
     qry = ServiceOperationQuery(self, "addTemplateFile", {
         "urlOfFile": url_of_file,
         "templateFileType": template_file_type
     })
     self.context.add_query(qry, file_new)
     self.add_child(file_new)
     return file_new
示例#7
0
def download_file_alt(context, file_url, local_path):
    """
    Download a file by server relative url
    :param local_path: str
    :param file_url: str
    :type context: ClientContext
    """
    response = File.open_binary(context, file_url)
    response.raise_for_status()
    with open(local_path, "wb") as local_file:
        local_file.write(response.content)
 def add(self, file_creation_information):
     """Creates a File resource"""
     target_file = File(self.context)
     self.add_child(target_file)
     qry = ServiceOperationQuery(
         self, "add", {
             "overwrite": file_creation_information.overwrite,
             "url": file_creation_information.url
         }, file_creation_information.content, None, target_file)
     self.context.add_query(qry)
     return target_file
 def add(self, file_creation_information):
     """Creates a File resource"""
     file_new = File(self.context)
     qry = ClientQuery.service_operation_query(
         self, ActionType.PostMethod, "add", {
             "overwrite": file_creation_information.overwrite,
             "url": file_creation_information.url
         }, file_creation_information.content)
     self.context.add_query(qry, file_new)
     self.add_child(file_new)
     return file_new
 def add(self, file_creation_information):
     """Creates a File resource"""
     file_new = File(self.context)
     qry = ServiceOperationQuery(
         self, "add", {
             "overwrite": file_creation_information.overwrite,
             "url": file_creation_information.url
         }, file_creation_information.content)
     self.context.add_query(qry, file_new)
     self.add_child(file_new)
     return file_new
    def __init__(self, container, file_creation_information):
        """

        :type file_creation_information: office365.sharepoint.file_creation_information.FileCreationInformation
        :type container: FileCollection
        """
        self._return_type = File(container.context)
        super().__init__(
            container, "add", {
                "overwrite": file_creation_information.overwrite,
                "url": file_creation_information.url
            }, file_creation_information.content, None, self._return_type)
        container.add_child(self._return_type)
示例#12
0
    def add(self, attachment_file_information):
        """Creates an attachment"""
        if isinstance(attachment_file_information, dict):
            attachment_file_information = AttachmentfileCreationInformation(
                attachment_file_information.get('filename'),
                attachment_file_information.get('content'))

        file_new = File(self.context)
        qry = ClientQuery.service_operation_query(
            self, ActionType.PostMethod, "add", {
                "filename": attachment_file_information.filename,
            }, attachment_file_information.content)
        self.context.add_query(qry, file_new)
        self.add_child(file_new)
        return file_new
示例#13
0
    def add(self, attachment_file_information):
        """Creates an attachment"""
        if isinstance(attachment_file_information, dict):
            attachment_file_information = AttachmentfileCreationInformation(
                attachment_file_information.get('filename'),
                attachment_file_information.get('content'))

        target_file = File(self.context)
        self.add_child(target_file)
        qry = ServiceOperationQuery(
            self, "add", {
                "filename": attachment_file_information.filename,
            }, attachment_file_information.content, None, target_file)
        self.context.add_query(qry)
        return target_file
def download_file(context):
    path = "../../tests/data/SharePoint User Guide.docx"
    response = File.open_binary(context, "Shared Documents/SharePoint User Guide.docx")
    response.raise_for_status()
    with open(path, "wb") as local_file:
        local_file.write(response.content)
示例#15
0
 def test_3_download_file(self):
     """Test file upload operation"""
     response = File.open_binary(self.context, self.report_file_url)
     self.assertEqual(response.content, '"' + self.report_content + '"')
示例#16
0
 def test_2_update_file(self):
     """Test file upload operation"""
     File.save_binary(self.context, self.report_file_url, self.report_content)
示例#17
0
def download_file(context):
    response = File.open_binary(
        context, "/Shared Documents/SharePoint User Guide.docx")
    with open("./data/SharePoint User Guide.docx", "wb") as local_file:
        local_file.write(response.content)
示例#18
0
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
import pandas as pd
url = 'https://s.'

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user('e.com', 'xxx')
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "/veille.csv")
with open("./veille.csv", "wb") as local_file:
    local_file.write(response.content)


 def test4_get_file_from_absolute_url(self):
     file_abs_url = self.client.base_url + self.__class__.target_file.properties[
         'ServerRelativeUrl']
     file = File.from_url(file_abs_url).with_credentials(
         self.credentials).load().execute_query()
     self.assertIsNotNone(file.serverRelativeUrl)
    def download_files(self, src, dest, file_pattern, period, worksheet_names,
                       table_names, exec_id, script_path):
        #print ("In download_files")
        """
        Download all the files in a src SharePoint directory to local dest directory.

        :param src: Source SharePoint folder from which to download files
        :type src: string
        :param dest: Destination local directory into which to download files
        :type dest: string
        :return: Number of files downloaded
        """
        #
        # Create some useful variables from download source folder
        # Incoming src variable should be like: /sites/SageDS/Shared Documents/NPT/Oct 2018
        #
        _base_path = os.path.dirname(
            src
        )  # Grab the head of dir (ie. /sites/SageDS/Shared Documents/NPT)
        #print("_base_path :", _base_path)
        _date_folder = os.path.basename(
            src)  # Grab the base of dir (ie. Oct 2018)
        #print("_date_folder :", _date_folder)
        _move_to_path = "{}/Archived/{}/".format(_base_path, _date_folder)
        ##print("_move_to_path :", _move_to_path)

        self.log.debug(
            "download_files: Starting src:{} base:{} date:{}".format(
                src, _base_path, _date_folder))

        #
        # Make sure Archived folder exists in SharePoint before moving on
        #
        self.check_archived_folder(src)

        #
        # Get the list of filenames in the SharePoint folder
        #
        _files = self.list_files(src)

        ##print ("_files :", _files)

        #
        # Keep a count of the number of files downloaded
        #
        _num_downloaded = 0

        #
        # Make sure there's something to download
        #
        if not len(_files):
            self.log.info(
                "download_files: No files found to download from {} of {}".
                format(src, self.url))
            return _num_downloaded

        self.log.info(
            "files_found: Number of files {} found from {} of {}".format(
                len(_files), src, self.url))

        #
        # Walk the list of files, downloading each one into destination directory
        #

        for _f, _fct in _files.items():
            #print ("In _files Loop")
            _spn = "{}/{}".format(src, _f)  # Source pathname
            ##print ('_spn =', _spn)
            _dpn = os.path.join(dest, _f)  # Destination pathname
            ##print ('_dpn =', _dpn)

            _upper_file_name = _f.upper()
            ##print ('_upper_file_name =', _upper_file_name)
            # self.log.info('_upper_file_name = ' + _upper_file_name)
            _upper_file_pattern = file_pattern.upper()
            # self.log.info('_upper_file_pattern = ' + _upper_file_pattern)
            ##print ('_upper_file_pattern =', _upper_file_pattern)

            file_pattern_match = re.findall(_upper_file_pattern,
                                            _upper_file_name)
            # self.log.info('file_pattern_match = ' + str(file_pattern_match))
            if file_pattern_match:
                ##print ("Found files with the pattern {}".format(file_pattern))
                self.log.debug("download_files: Downloading {} to {}".format(
                    _spn, _dpn))
                # self.log.info("download_files: Downloading {} to {}".format(_spn, _dpn))
                # Insert Log
                proc_name = _f
                statement_type = "Download File -> " + _f
                table_name = 'null'
                # success_failure = "Download Complete"
                # self.log.info('exec_id = ' + str(exec_id) + ' proc_name = ' + proc_name + ' statement_type = '
                #               + statement_type)
                qrs.insert_log(exec_id, proc_name, statement_type, table_name,
                               'null', "Started")

                #
                # Download the file
                #
                try:
                    with open(_dpn, 'wb') as _ofd:
                        _response = File.open_binary(self._ctx, _spn)
                        _ofd.write(_response.content)
                    # success_failure = "Download Complete"
                    qrs.insert_log(exec_id, proc_name, statement_type,
                                   table_name, 'null', "Completed")
                except Exception as e:
                    _msg = "{} error encountered creating destination file {}".format(
                        e, _dpn)
                    log.critical(_msg)
                    raise ValueError(_msg)
                dct = datetime.fromtimestamp(os.path.getmtime(_dpn))
                ##print ("down load time : " ,dct)
                ##print ("type of _fct = ", type(_fct))
                #_fp.append(dct)
                ##print ("_fp = ", _fp)
                #valid_files_dict[_f] = _fp
                ##print ("valid_files_dict = ", valid_files_dict)

                sqlcmd = "INSERT INTO [T_STG_FILES_DOWNLOADED]" \
                         + "([FILE_NAME_PATTERN],[FILE_NAME],[DOWNLOADED_TIME],[PERIOD],[PROJECT],[FILE_CREATION_TIME]) " \
                         + "VALUES(" \
                         + "'" + file_pattern +  "'," \
                         +  "'" + _f +  "'," \
                         + "CAST(left('" + str(dct) + "',23)" + " AS datetime),'" \
                         + period +  "'," \
                         +"'" + qrs.Project_Name + "'," \
                         + "CAST ('" +_fct + "' AS datetime))"
                ##print('sqlcmd = ', sqlcmd)
                qrs.dml_cursor(sqlcmd)
                LOG.info(
                    "Uploaded to data base strarts:Work sheet {} Table: {} ".
                    format(worksheet_names, table_names))
                os_cmd = script_path + 'Opex_dynamic_upload_to_DB.py ' + str(
                    exec_id
                ) + ' "' + _dpn + '" "' + table_names + '" "' + worksheet_names + '"'
                print('Initiating System Command -> \n' + os_cmd)
                qrs.run_os_cmd(os_cmd)
                LOG.info(
                    "All Work sheet {} Uploaded to Table: {} was successful".
                    format(worksheet_names, table_names))
                #print ("file = " ,  "'" + dest + "\\" +_f + "'")
                #print ("worksheet_names = "  ,  "'" + worksheet_names+ "'")
                #print ("table_names = "  ,  "'" + table_names + "'")

                _num_downloaded += 1

                #
                # File downloaded, move it to Archived folder
                #
                _to_path = _move_to_path + _f  #commented by Mukesh not archieve for testing

                self.log.debug("download_files: Moving {} to {}".format(
                    _spn, _to_path))
                #
                # Moving the file
                #
                try:
                    print("in moving block")
                    LOG.info("\n Sharepoint:File {} has been archived in {} ".
                             format(self._ctx, _spn, _to_path))
                    _resp = File.move(
                        self._ctx, _spn, _to_path
                    )  #commented by Mukesh not archieve for testing
                    ##print ("in moving block after MOve")
                except Exception as e:
                    _msg = "{} error encountered moving {} to  file {}".format(
                        e, _spn, _to_path)
                    log.critical(_msg)
                    raise ValueError(_msg)
                #_resp = File.move(self._ctx, _spn, _to_path)

                #
                # Was move successful
                #
                if _resp.status_code != 200:
                    self.log.error(
                        "download_files: Error: {} moving {} to {}".format(
                            _resp.status_code, _spn, _to_path))
                else:
                    self.log.debug("download_files: Moved {} to {}".format(
                        _spn, _to_path))

        #
        # Return count of files downloaded
        #
        return _num_downloaded
示例#21
0
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File

app_settings = {
    'url': 'https://xxxx.sharepoint.com/sites/testprivate',
    'client_id': '3601b9xxxxf8b67e3a',
    'client_secret': '4qr3urbxxxxEUESzc13Ys=',
}

ctx_auth = AuthenticationContext(url=app_settings['url'])
ctx_auth.acquire_token_for_app(client_id=app_settings['client_id'],
                               client_secret=app_settings['client_secret'])

ctx = ClientContext(app_settings['url'], ctx_auth)

path = "D:\dest.txt"
response = File.open_binary(
    ctx, "/sites/testprivate/Shared%20Documents/source.txt")
response.raise_for_status()
with open(path, "wb") as local_file:
    local_file.write(response.content)

print('f')
 def get_by_url(self, url):
     """Retrieve File object by url"""
     return File(
         self.context,
         ResourcePathServiceOperation(self.context, self.resource_path,
                                      "GetByUrl", [url]))
示例#23
0
import os
import tempfile

from office365.runtime.auth.userCredential import UserCredential
from office365.sharepoint.file import File
from settings import settings

abs_file_url = "{site_url}sites/team/Shared Documents/sample.docx".format(
    site_url=settings.get('url'))
user_credentials = UserCredential(
    settings.get('user_credentials').get('username'),
    settings.get('user_credentials').get('password'))

file_name = os.path.basename(abs_file_url)
with tempfile.TemporaryDirectory() as local_path:
    with open(os.path.join(local_path, file_name), 'wb') as local_file:
        file = File.from_url(abs_file_url).with_credentials(
            user_credentials).download(local_file).execute_query()
    print("'{0}' file has been downloaded into {1}".format(
        file.serverRelativeUrl, local_file.name))
示例#24
0
def qr_batch():

    print("")
    print(
        "The batch QR code tool is used to automatically create multiple QR codes by referencing a .csv file. The tool will automatically create QR codes for each participant's email address and save each QR image to the SharePoint site. \n"
    )
    input("Press Enter to Continue \n")
    # this code creates a batch of QR codes from a csv file stored in the local directory
    # QR code image size and input filename can be modified below
    resp = File.open_binary(ctx, relative_url)
    status = resp.status_code

    if status == 404:
        print(
            "The batch file '" + relative_url +
            "' doesn't exist. Please copy 'names.csv' to the sharepoint site.")
        return False

    with open(qrbatchfile, 'wb') as output_file:
        output_file.write(resp.content)

    with open(qrbatchfile) as csvfile:
        reader = csv.reader(csvfile)

        for row in reader:
            labeldata = row[0]

            qr = qrcode.QRCode(
                version=1,
                error_correction=qrcode.constants.ERROR_CORRECT_L,
                box_size=10,
                border=4)

            qr.add_data(labeldata)
            qr.make(fit=True)
            print("Creating QR code: " + labeldata)

            # draw QR image

            img = qr.make_image()
            qrfile = labeldata + ".jpg"
            img.save(qrfile)

            # open QR image and add qr data as name

            img = Image.open(qrfile)
            draw = ImageDraw.Draw(img)
            font = ImageFont.truetype("arial", 18)
            color = 0
            draw.text((37, 10), labeldata, font=font, fill=color)
            img.save(qrfile)

            # upload file

            with open(qrfile, 'rb') as content_file:
                file_content = content_file.read()
            upload_file(ctx, file_content, qrfile, qrfolder)
            os.remove(qrfile)

    os.remove(qrbatchfile)
    print("Success! \n")
 def get_by_id(self, _id):
     """Gets the File with the specified ID."""
     return File(
         self.context,
         ResourcePathServiceOperation(self.context, self.resource_path,
                                      "getById", [_id]))
 def test_3_download_file(self):
     """Test file upload operation"""
     response = File.open_binary(self.context, self.report_file_url)
     str_output_content = response.content.decode("utf-8")
     self.assertEqual(str_output_content,
                      '"{0}"'.format(self.report_content))