def refresh(self): OfficeDocument.refresh( self ) try: self.updateCharts() except: traceback.print_exc( file=sys.stdout ) pass
def createDataSource( DataProvider, CellRangeRepresentation, DataRowSource, FirstCellAsLabel=True, HasCategories=True ): props = [] props.append( OfficeDocument._makeProperty( 'CellRangeRepresentation', CellRangeRepresentation ) ) props.append( OfficeDocument._makeProperty( 'DataRowSource', DataRowSource ) ) props.append( OfficeDocument._makeProperty( 'FirstCellAsLabel', FirstCellAsLabel ) ) props.append( OfficeDocument._makeProperty( 'HasCategories', HasCategories ) ) props = tuple( props ) ds = DataProvider.createDataSource( props ) return ds
def __init__( self, odt='', type='odt' ): self.document = OfficeDocument.createDocument( type ) if odt != '': #copy filt to temporary document, becasuse two webservice users using the same file causes problems os.path.exists( odt ) self.origionalPath = odt self.documentPath = self._getTempFile( type ) shutil.copyfile( odt, self.documentPath ) self.document.open( self.documentPath ) pyMailMerge.documentsOpen[ self.documentPath ] = self.documentPath
def updateCharts( self ): #get all of the tables in the document, we'll need them later tables = self.oodocument.getTextTables() #loop through each of the draw pages in this document count = 0 drawPage = self.oodocument.getDrawPage() e = drawPage.createEnumeration() while e.hasMoreElements(): x = e.nextElement() count += 1 try: #get the chart object embedded = x.getEmbeddedObject() skip = False except: skip = True if skip is False: #get a tuple of all the ranges used in the chart used = embedded.getUsedRangeRepresentations() #eg, (u"Table1.A2:A4", u"Table1.B1:B1", u"Table1.B2:B4", u"Table1.C1:C1", u"Table1.C2:C4" ) #need to pass these properties to the new data source we will create props = 'DataSourceLabelsInFirstRow', 'DataSourceLabelsInFirstColumn' DataSourceLabelsInFirstRow, DataSourceLabelsInFirstColumn = embedded.getPropertyValues( props ) #get the table the range is based off of tablename = used[0].split('.')[0] try: table = tables.getByName( tablename ) except: table = None if table is not None: #determine the cell name that we are going to start out new range with cellName = used[0].split('.')[1].split(":")[0] row, col = self._convertCellNameToCellPositions( cellName ) if DataSourceLabelsInFirstRow and row > 1: row = int(row)-1 startCellName = "%s%s" % ( re.sub( '\d+', '', cellName ), row ) #determine the cell name that we are going to end our new range with lastcolumn = used[ len(used)-1 ].split(":")[1] columnName = re.sub( '\d+', '', lastcolumn ) #build the representation string ie. "Table1.A1:C4" representation = "%s.%s:%s%s" % (tablename, startCellName, columnName, table.Rows.getCount() ) #create a new re.sub( '\d+', '', cellName ) out of the representation we built dp = embedded.getDataProvider() ds = WriterDocument.createDataSource( dp, representation, COLUMNS, DataSourceLabelsInFirstRow, DataSourceLabelsInFirstColumn ) #get the diagram object and tell it to use our new data source diagram = embedded.getFirstDiagram() try: #this is a much better way of updating the chart, however I have a system # where the setDiagramData method does not exist, and throws an exception # never fear.... I have a backup plan. prop = OfficeDocument._makeProperty( "HasCategories", True ), diagram.setDiagramData( ds, prop ) except: #here is the backup plan, it's a lot more convoluted sequences = ds.getDataSequences() new_data = [] for x in sequences: values = [] for x in x.getValues().getData(): try: values.append( float( x.replace('$','').replace( ',', '' ).replace( ' ', '' ) ) ) except: pass if len( values ) > 0: new_data.append( tuple( values ) ) #need to invert the matrix new_data = WriterDocument.invertTuple( new_data ) #set the data embedded.getData().setData( new_data ) #new get the data for the updated legends pos_row, pos_col = WriterDocument._convertCellNameToCellPositions( used[0].split('.')[1] ) new_legends = [] for x in xrange( pos_row-1, table.getRows().getCount()-1 ): new_legends.append( table.getCellByPosition( pos_col, x+pos_row ).Text.getString() ) #set the updated legend data embedded.getData().setRowDescriptions( tuple(new_legends) )