Example #1
0
    def convert(self, inputFile, outputFile):
        """
        Convert the input file (a spreadsheet) to a CSV file.
        """

        # Start openoffice if needed.
        if not self.desktop:
            if not self.oorunner:
                self.oorunner = ooutils.OORunner()

            self.desktop = self.oorunner.connect()

        inputUrl  = uno.systemPathToFileUrl(os.path.abspath(inputFile))
        outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFile))
        document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=True))

        try:
            # Additional property option:
            #   FilterOptions="59,34,0,1"
            #     59 - Field separator (semicolon), this is the ascii value.
            #     34 - Text delimiter (double quote), this is the ascii value.
            #      0 - Character set (system).
            #      1 - First line number to export.
            #
            # For more information see:
            #   http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
            #
            document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="Text - txt - csv (StarCalc)"))
        finally:
            document.close(True)
Example #2
0
    def convert(self, inputFile, outputFile):
        """
        Convert the input file (a spreadsheet) to a CSV file.
        """

        # Start openoffice if needed.
        if not self.desktop:
            if not self.oorunner:
                self.oorunner = ooutils.OORunner()

            self.desktop = self.oorunner.connect()

        inputUrl  = uno.systemPathToFileUrl(os.path.abspath(inputFile))
        outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFile))
        document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=True))

        try:
            # Additional property option:
            #   FilterOptions="59,34,0,1"
            #     59 - Field separator (semicolon), this is the ascii value.
            #     34 - Text delimiter (double quote), this is the ascii value.
            #      0 - Character set (system).
            #      1 - First line number to export.
            #
            # For more information see:
            #   http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
            #
            document.storeToURL(outputUrl, ooutils.oo_properties(FilterName="Text - txt - csv (StarCalc)"))
        finally:
            document.close(True)
Example #3
0
    def convert(self, inputFile, outputFile, SheetName = None, DelimiterInAscii = 9):
        """
        Convert the input file (a spreadsheet) to a CSV file.
        @param DelimiterInAscii: the delimiter. Default value \t (Ascii: 9) 
        """

        # Start openoffice if needed.
        if not self.desktop:
            if not self.oorunner:
                self.oorunner = ooutils.OORunner()

            self.desktop = self.oorunner.connect()

        inputUrl  = uno.systemPathToFileUrl(os.path.abspath(inputFile))
        outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFile))
        
        try:
            document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=False))
        except IllegalArgumentException, e:
            raise IOError( "Failed to open '%s': %s" % (inputFile, e.Message) )
Example #4
0
    def convert(self, inputFile, outputFile, verbose=False):
        """
        Convert the input file (a spreadsheet) to a CSV file.

        The input file name can contain a sheet specification to specify a particular sheet.
        The sheet specification is either a number or a sheet name.
        The sheet specification is appended to the file name separated by a colon
        or an at sign: ":" or "@".

        If the output file name contains a %d or %s format specifier, then all the sheets
        in the input file are converted, otherwise only the first sheet is converted.

        If the output file name contains a %d format specifier then the sheet number
        is used when formatting the output file name.
        The format can contain a width specifier (eg %02d).

        If the output file name contains a %s specifier then the sheet name is used
        when formatting the output file name.
        """

        # Start openoffice if needed.
        if not self.desktop:
            if not self.oorunner:
                self.oorunner = ooutils.OORunner()

            self.desktop = self.oorunner.connect()

        # Check for sheet specification in input file name.
        match = re.search(r'^(.*)[@:](.*)$', inputFile)
        if os.path.exists(inputFile) or not match:
            inputUrl   = uno.systemPathToFileUrl(os.path.abspath(inputFile))
            inputSheet = '1'   # Convert fist sheet.
        else:
            inputUrl   = uno.systemPathToFileUrl(os.path.abspath(match.group(1)))
            inputSheet = match.group(2)


        # NOTE:
        #   Sheet activation does not work properly when Hidden is specified.
        #   Although the sheet does become the active sheet, it's not the sheet that
        #   gets saved if the spreadsheet is loaded with Hidden=True.
        #
        #   Removing Hidden=True doesn't seem to change anything: nothing appears
        #   on the screen regardless of the Hidden value.
        #
        # document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=True))
        document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties())

        try:
            props = ooutils.oo_properties(FilterName="Text - txt - csv (StarCalc)")
            #
            # Another useful property option:
            #   FilterOptions="59,34,0,1"
            #     59 - Field separator (semicolon), this is the ascii value.
            #     34 - Text delimiter (double quote), this is the ascii value.
            #      0 - Character set (system).
            #      1 - First line number to export.
            #
            # For more information see:
            #   http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options

            # To convert a particular sheet, the sheet needs to be active.
            # To activate a sheet we need the spreadsheet-view, to get the spreadsheet-view
            # we need the spreadsheet-controller, to get the spreadsheet-controller
            # we need the spreadsheet-model.
            #
            # The spreadsheet-model interface is available from the document object.
            # The spreadsheet-view interface is available from the controller.
            #
            controller = document.getCurrentController()
            sheets     = document.getSheets()

            # If the output file name contains a %d or %s format specifier, convert all sheets.
            # Use the sheet number if the format is %d, otherwise the sheet name.
            dfmt = re.search(r'%[0-9]*d', outputFile)
            sfmt = re.search(r'%s', outputFile)

            if dfmt  or  sfmt:
                i = 0
                while i < sheets.getCount():
                    # Activate the sheet.
                    sheet = sheets.getByIndex(i)
                    controller.setActiveSheet(sheet)

                    # Create output file name.
                    if dfmt:
                        ofile = outputFile % (i+1)
                    else:
                        ofile = outputFile % sheet.getName().replace(' ', '_')

                    if verbose: print("    %s" % ofile)

                    # Save the sheet to the output file.
                    outputUrl = uno.systemPathToFileUrl(os.path.abspath(ofile))
                    document.storeToURL(outputUrl, props)
                    i += 1

            else:
                # Activate the sheet to be converted.
                if re.search(r'^\d+$', inputSheet):
                    sheet = sheets.getByIndex(int(inputSheet)-1)
                else:
                    sheet = sheets.getByName(inputSheet)

                controller.setActiveSheet(sheet)
                outputUrl = uno.systemPathToFileUrl(os.path.abspath(outputFile))
                document.storeToURL(outputUrl, props)
        finally:
            if document: document.close(True)
Example #5
0
    def convert(self, inputFile, outputFile, verbose=False):
        """
        Convert the input file (a spreadsheet) to a CSV file.

        The input file name can contain a sheet specification to specify a particular sheet.
        The sheet specification is either a number or a sheet name.
        The sheet specification is appended to the file name separated by a colon
        or an at sign: ":" or "@".

        If the output file name contains a %d or %s format specifier, then all the sheets
        in the input file are converted, otherwise only the first sheet is converted.

        If the output file name contains a %d format specifier then the sheet number
        is used when formatting the output file name.
        The format can contain a width specifier (eg %02d).

        If the output file name contains a %s specifier then the sheet name is used
        when formatting the output file name.
        """

        # Start openoffice if needed.
        if True:
            self.connection = None
            self.pipe = None
            self.server = 'localhost'
            self.port = '2002'
            self.verbose = 0
            if not self.connection:
                if not self.pipe:
                    self.connection = "socket,host=%s,port=%s;urp;StarOffice.ComponentContext" % (
                        self.server, self.port)

    #               self.connection = "socket,host=%s,port=%s;urp;" % (self.server, self.port)
                else:
                    self.connection = "pipe,name=%s;urp;StarOffice.ComponentContext" % (
                        self.pipe)
                if self.verbose >= 3:
                    print >> sys.stderr, 'Connection type: %s' % self.connection

            self.context = uno.getComponentContext()
            self.svcmgr = self.context.ServiceManager
            resolver = self.svcmgr.createInstanceWithContext(
                "com.sun.star.bridge.UnoUrlResolver", self.context)
            unocontext = resolver.resolve("uno:%s" % self.connection)
            unosvcmgr = unocontext.ServiceManager
            self.desktop = unosvcmgr.createInstanceWithContext(
                "com.sun.star.frame.Desktop", unocontext)
        else:
            if not self.desktop:
                if not self.oorunner:
                    self.oorunner = ooutils.OORunner()

                self.desktop = self.oorunner.connect()

        # Check for sheet specification in input file name.
        match = re.search(r'^(.*)[@:](.*)$', inputFile)
        if os.path.exists(inputFile) or not match:
            inputUrl = uno.systemPathToFileUrl(os.path.abspath(inputFile))
            inputSheet = '1'  # Convert fist sheet.
        else:
            inputUrl = uno.systemPathToFileUrl(os.path.abspath(match.group(1)))
            inputSheet = match.group(2)

        # NOTE:
        #   Sheet activation does not work properly when Hidden is specified.
        #   Although the sheet does become the active sheet, it's not the sheet that
        #   gets saved if the spreadsheet is loaded with Hidden=True.
        #
        #   Removing Hidden=True doesn't seem to change anything: nothing appears
        #   on the screen regardless of the Hidden value.
        #
        # document  = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, ooutils.oo_properties(Hidden=True))
        document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0,
                                                     ooutils.oo_properties())
        outputFilenames = []

        try:
            props = ooutils.oo_properties(
                FilterName="Text - txt - csv (StarCalc)")
            #
            # Another useful property option:
            #   FilterOptions="59,34,0,1"
            #     59 - Field separator (semicolon), this is the ascii value.
            #     34 - Text delimiter (double quote), this is the ascii value.
            #      0 - Character set (system).
            #      1 - First line number to export.
            #
            # For more information see:
            #   http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options

            # To convert a particular sheet, the sheet needs to be active.
            # To activate a sheet we need the spreadsheet-view, to get the spreadsheet-view
            # we need the spreadsheet-controller, to get the spreadsheet-controller
            # we need the spreadsheet-model.
            #
            # The spreadsheet-model interface is available from the document object.
            # The spreadsheet-view interface is available from the controller.
            #
            controller = document.getCurrentController()
            sheets = document.getSheets()

            # If the output file name contains a %d or %s format specifier, convert all sheets.
            # Use the sheet number if the format is %d, otherwise the sheet name.
            dfmt = re.search(r'%[0-9]*d', outputFile)
            sfmt = re.search(r'%s', outputFile)

            if dfmt or sfmt:
                i = 0
                while i < sheets.getCount():
                    # Activate the sheet.
                    sheet = sheets.getByIndex(i)
                    controller.setActiveSheet(sheet)

                    # Create output file name.
                    if dfmt:
                        ofile = outputFile % (i + 1)
                    else:
                        ofile = outputFile % sheet.getName().replace(' ', '_')

                    if verbose: print "    %s" % ofile

                    # Save the sheet to the output file.
                    outputFilenames.append(ofile)
                    outputUrl = uno.systemPathToFileUrl(os.path.abspath(ofile))
                    document.storeToURL(outputUrl, props)

                    i += 1

            else:
                # Activate the sheet to be converted.
                if re.search(r'^\d+$', inputSheet):
                    sheet = sheets.getByIndex(int(inputSheet) - 1)
                else:
                    sheet = sheets.getByName(inputSheet)

                controller.setActiveSheet(sheet)
                outputFilenames.append(outputFile)
                outputUrl = uno.systemPathToFileUrl(
                    os.path.abspath(outputFile))
                document.storeToURL(outputUrl, props)

            return outputFilenames
        finally:
            if document: document.close(True)