def insertTable(self, stringdata): def insertTextIntoCell(table, cellName, text, color): tableText = table.getCellByName(cellName) cursor = tableText.createTextCursor() cursor.setPropertyValue("CharColor", color) tableText.setString(text) table = self.document.createInstance("com.sun.star.text.TextTable") data = json.loads(stringdata) rows = len(data.keys()) columns = len(data.values()[0]) table.initialize(rows, columns) self.document.Text.insertTextContent(self.cursor, table, 0) rows = table.Rows table.setPropertyValue("BackTransparent", uno.Bool(0)) table.setPropertyValue("BackColor", WHITE) row = rows.getByIndex(0) row.setPropertyValue("BackTransparent", uno.Bool(0)) row.setPropertyValue("BackColor", GREY) for roworder, rowdata in data.iteritems(): for index, rowtext in enumerate(rowdata): tableposition = "%s%d" % (chr(65 + index), int(roworder)) if tableposition.endswith('1'): insertTextIntoCell(table, tableposition, rowtext, WHITE) else: insertTextIntoCell(table, tableposition, rowtext, BLACK)
def draw_heading(doc): '''Add the heading text. Warning: Steps below must be performed in order''' # Step1: Create the text box shape shape_position = Point() shape_position.X = 8000 shape_position.Y = 1500 shape_size = Size() shape_size.Width= 16000 shape_size.Height= 1000 textbox = make_text_shape(doc, shape_position, shape_size) # Step 2: Add the shape to the draw document doc.DrawPages.page1.add(textbox) # Step 3: get the object getText() and pass the heading string textboxtext = textbox.getText() textboxtext.setString("Python House") # Make modifications to the text textboxtext.TextAutoGrowHeight = True textboxtext.TextAutoGrowWidth = True #textboxtext.CharFontName = "Times New Roman" #textboxtext.CharFontName = "Purisa" #textboxtext.CharHeight = 80 #textboxtext.CharColor = 0xff00ff # Alternative method of setting properties textboxtext.setPropertyValue("CharFontName", "Purisa") textboxtext.setPropertyValue("CharHeight", 80) textboxtext.setPropertyValue("CharColor", 0xff00ff) textboxtext.setPropertyValue("CharShadowed", uno.Bool(1))
def insertImage(self, image_name, width=None, height=None, vposition=None): """ Add image to a document Image is added to the active cursor position @param image_name Image filename """ oShape = self.doc.createInstance("com.sun.star.text.TextGraphicObject") oShape.GraphicURL = image_name oShape.AnchorType = AS_CHARACTER if width is not None: oShape.Width = width if height is not None: oShape.Height = height if vposition is not None: oShape.VertOrient = uno.getConstantByName( "com.sun.star.text.VertOrientation.CHAR_CENTER") oShape.VertOrientRelation = uno.getConstantByName( "com.sun.star.text.RelOrientation.TEXT_LINE") oShape.VertOrientPosition = vposition self.cursor.Text.insertTextContent(self.cursor, oShape, uno.Bool(0))
def createframe(doc,text,cursor): '''Two lines of text in a frame''' textFrame = doc.createInstance("com.sun.star.text.TextFrame") textFrame.setSize(Size(15000,400)) # Done in import stage #from com.sun.star.text.TextContentAnchorType import AS_CHARACTER textFrame.setPropertyValue("AnchorType" , AS_CHARACTER) text.insertTextContent(cursor, textFrame, 0) textInTextFrame = textFrame.getText() cursorInTextFrame = textInTextFrame.createTextCursor() textInTextFrame.insertString(cursorInTextFrame, "The first line in the newly created text frame.", 0) textInTextFrame.insertString(cursorInTextFrame, "\nWith this second line the height of the frame raises.",0) text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0) cursor.setPropertyValue("CharColor", 65536) cursor.setPropertyValue("CharShadowed", uno.Bool(0))
def testBool( self ): self.failUnless( uno.Bool(1) , "uno.Bool true test" ) self.failUnless( not uno.Bool(0) , "uno.Bool false test" ) self.failUnless( uno.Bool( "true") , "uno.Bool true1 test" ) self.failUnless( not uno.Bool( "false") , "uno.Bool true1 test" ) self.tobj.Bool = uno.Bool(1) self.failUnless( self.tobj.Bool , "bool true attribute test" ) self.tobj.Bool = uno.Bool(0) self.failUnless( not self.tobj.Bool , "bool true attribute test" ) # new boolean semantic self.failUnless( id( self.tobj.transportAny( True ) ) == id(True) , "boolean preserve test") self.failUnless( id( self.tobj.transportAny( False ) ) == id(False) , "boolean preserve test" ) self.failUnless( id( self.tobj.transportAny(1) ) != id( True ), "boolean preserve test" ) self.failUnless( id( self.tobj.transportAny(0) ) != id( False ), "boolean preserve test" )
text = doc.Text cursor = text.createTextCursor() text.insertString(cursor, "The first line in the newly created text document.\n", 0) text.insertString(cursor, "Now we are in the second line\n", 0) # create a text table table = doc.createInstance("com.sun.star.text.TextTable") # with 4 rows and 4 columns table.initialize(4, 4) text.insertTextContent(cursor, table, 0) rows = table.Rows table.setPropertyValue("BackTransparent", uno.Bool(0)) table.setPropertyValue("BackColor", 13421823) row = rows.getByIndex(0) row.setPropertyValue("BackTransparent", uno.Bool(0)) row.setPropertyValue("BackColor", 6710932) textColor = 16777215 insertTextIntoCell(table, "A1", "FirstColumn", textColor) insertTextIntoCell(table, "B1", "SecondColumn", textColor) insertTextIntoCell(table, "C1", "ThirdColumn", textColor) insertTextIntoCell(table, "D1", "SUM", textColor) values = ((22.5, 21.5, 121.5), (5615.3, 615.3, -615.3), (-2315.7, 315.7, 415.7)) table.getCellByName("A2").setValue(22.5)
def createTable(): """creates a new writer document and inserts a table with some data (also known as the SWriter sample)""" ctx = uno.getComponentContext() smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) # open a writer document doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ()) text = doc.Text cursor = text.createTextCursor() text.insertString(cursor, "The first line in the newly created text document.\n", 0) text.insertString(cursor, "Now we are in the second line\n", 0) # create a text table table = doc.createInstance("com.sun.star.text.TextTable") # with 4 rows and 4 columns table.initialize(4, 4) text.insertTextContent(cursor, table, 0) rows = table.Rows table.setPropertyValue("BackTransparent", uno.Bool(0)) table.setPropertyValue("BackColor", 13421823) row = rows.getByIndex(0) row.setPropertyValue("BackTransparent", uno.Bool(0)) row.setPropertyValue("BackColor", 6710932) textColor = 16777215 insertTextIntoCell(table, "A1", "FirstColumn", textColor) insertTextIntoCell(table, "B1", "SecondColumn", textColor) insertTextIntoCell(table, "C1", "ThirdColumn", textColor) insertTextIntoCell(table, "D1", "SUM", textColor) values = ((22.5, 21.5, 121.5), (5615.3, 615.3, -615.3), (-2315.7, 315.7, 415.7)) table.getCellByName("A2").setValue(22.5) table.getCellByName("B2").setValue(5615.3) table.getCellByName("C2").setValue(-2315.7) table.getCellByName("D2").setFormula("sum <A2:C2>") table.getCellByName("A3").setValue(21.5) table.getCellByName("B3").setValue(615.3) table.getCellByName("C3").setValue(-315.7) table.getCellByName("D3").setFormula("sum <A3:C3>") table.getCellByName("A4").setValue(121.5) table.getCellByName("B4").setValue(-615.3) table.getCellByName("C4").setValue(415.7) table.getCellByName("D4").setFormula("sum <A4:C4>") cursor.setPropertyValue("CharColor", 255) cursor.setPropertyValue("CharShadowed", uno.Bool(1)) text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0) text.insertString(cursor, " This is a colored Text - blue with shadow\n", 0) text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0) textFrame = doc.createInstance("com.sun.star.text.TextFrame") textFrame.setSize(Size(15000, 400)) textFrame.setPropertyValue("AnchorType", AS_CHARACTER) text.insertTextContent(cursor, textFrame, 0) textInTextFrame = textFrame.getText() cursorInTextFrame = textInTextFrame.createTextCursor() textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0) textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.", 0) text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0) cursor.setPropertyValue("CharColor", 65536) cursor.setPropertyValue("CharShadowed", uno.Bool(0)) text.insertString(cursor, " That's all for now !!", 0)
def createDoc(): """creates a new writer document and inserts a table with some data (also known as the SWriter sample)""" ctx = uno.getComponentContext() smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) # open a writer document doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () ) text = doc.Text cursor = text.createTextCursor() text.insertString( cursor, "The first line in the newly created text document.\n", 0 ) text.insertString( cursor, "Now we are in the second line\n" , 0 ) # Create a text table noRows = 5 noCols = 4 table = daisyCreateTable(doc, cursor, noCols, noRows) rows = table.Rows table.setPropertyValue( "BackTransparent", uno.Bool(0) ) table.setPropertyValue( "BackColor", 13421823 ) row = rows.getByIndex(0) row.setPropertyValue( "BackTransparent", uno.Bool(0) ) row.setPropertyValue( "BackColor", 6710932 ) textColor = 16777215 insertTextIntoCell( table, "A1", "FirstColumn", textColor ) insertTextIntoCell( table, "B1", "SecondColumn", textColor ) insertTextIntoCell( table, "C1", "ThirdColumn", textColor ) insertTextIntoCell( table, "D1", "SUM", textColor ) values = ( (22.5,21.5,121.5), (5615.3,615.3,-615.3), (-2315.7,315.7,415.7) ) table.getCellByName("A2").setValue(22.5) table.getCellByName("B2").setValue(5615.3) table.getCellByName("C2").setValue(-2315.7) table.getCellByName("D2").setFormula("sum <A2:C2>") table.getCellByName("A3").setValue(21.5) table.getCellByName("B3").setValue(615.3) table.getCellByName("C3").setValue(-315.7) table.getCellByName("D3").setFormula("sum <A3:C3>") table.getCellByName("A4").setValue(121.5) table.getCellByName("B4").setValue(-615.3) table.getCellByName("C4").setValue(415.7) table.getCellByName("D4").setFormula("sum <A4:C4>") cursor.setPropertyValue( "CharColor", 255 ) cursor.setPropertyValue( "CharShadowed", uno.Bool(1) ) BR(doc, cursor) text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 ) BR(doc, cursor) textFrame = doc.createInstance( "com.sun.star.text.TextFrame" ) textFrame.setSize( Size(15000,400)) textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER ) text.insertTextContent( cursor, textFrame, 0 ) textInTextFrame = textFrame.getText() cursorInTextFrame = textInTextFrame.createTextCursor() textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 ) textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0) BR(doc, cursor) cursor.setPropertyValue( "CharColor", 65536 ) cursor.setPropertyValue( "CharShadowed", uno.Bool(0) ) daisyImportTextFile(doc, cursor, "/home/jacobw/textfile.txt") daisyPrintSineTable(doc, cursor) #daisyPrintDirList(doc, cursor) text.insertString( cursor, " That's all for now !!", 0 ) BR(doc, cursor) #daisyReadCsvFile(doc, cursor, "/home/jacobw/textfile.txt") text.insertString( cursor, "No, it wasnt!!", 0 ) BR(doc, cursor) # Save to file properties = ( PropertyValue('Overwrite', 0, True, 0), ) doc.storeToURL('file:///home/jacobw/test.odt', properties) #oTables = doc.TextTables #text.insertString( cursor, oTables.Length, 0 ) table.Rows.insertByIndex(table.Rows.getCount(),1) table.Rows.insertByIndex(table.Rows.getCount(),1) table.Rows.insertByIndex(table.Rows.getCount(),1) text.insertString( cursor, str(table.Rows.getCount()), 0) table2 = daisyCreateTable(doc, cursor, noCols, noRows) # for tableName in doc.TextTables: text.insertString( cursor, "There are "+ str(doc.TextTables.getCount()) + " tables in the document", 0) BR(doc, cursor) dWrite(doc, cursor, "The names of these are:") for tableId in doc.TextTables.getElementNames(): BR(doc, cursor) text.insertString( cursor, tableId, 0) BR(doc, cursor)
def list_books(*args): """ Function that creates the report in LibreOffice writer. """ ctx = uno.getComponentContext() smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) # open a writer document doc = desktop.loadComponentFromURL("private:factory/swriter", "default", 0, ()) pageStyle = doc.getStyleFamilies().getByName("PageStyles") defProp = pageStyle.getByName("Standard") defProp.setPropertyValue("IsLandscape", True) defProp.setPropertyValue("Width", 29700) defProp.setPropertyValue("Height", 21000) text = doc.Text cursor = text.createTextCursor() cursor.ParaStyleName = "Heading 1" text.insertString(cursor, "Κατάσταση Βιβλίων (Sqlite3)", 0) text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0) cursor.ParaStyleName = "Standard" # Change your path to the database. con, cur = login( os.path.join( os.path.expanduser("~"), '.config/libreoffice/4/user/Scripts/python/homelibrary.db')) qry = "select b.isbn, b.title, a.firstname || ' ' || a.surname as author, b.copies_standard, b.copies_avail from tbl_books b, tbl_author a where b.author_id=a.author_id order by b.author_id;" cur.execute(qry) result = cur.fetchall() con.close() result.insert(0, ("ISBN", "ΤΙΤΛΟΣ", "ΣΥΓΓΡΑΦΕΑΣ", "ΣΥΝΟΛΟ", "ΑΠΟΘΕΜΑ")) result_len = len(result) table_01 = doc.createInstance("com.sun.star.text.TextTable") table_01.initialize(result_len, len(result[0])) table_01.setName("TableBooks") text.insertTextContent(cursor, table_01, 0) tblColSeps = table_01.TableColumnSeparators tblColSeps[0].Position = 1400 tblColSeps[1].Position = 5500 tblColSeps[2].Position = 8000 tblColSeps[3].Position = 9000 table_01.TableColumnSeparators = tblColSeps table_01.setPropertyValue("HeaderRowCount", 1) table_01.setPropertyValue("RepeatHeadline", True) table_01.setPropertyValue("BackTransparent", uno.Bool(0)) table_01.setPropertyValue("BackColor", 13421823) table_01.setDataArray(result) rows_01 = table_01.Rows row_01 = rows_01.getByIndex(0) row_01.setPropertyValue("BackTransparent", uno.Bool(0)) # row_01.setPropertyValue("BackColor", 6710932) # table_01.Rows.insertByIndex(len(result), 1) oRange = table_01.getCellRangeByName("A2:E{0}".format(result_len)) oRange.CharHeight = 9.5 oRange = table_01.getCellRangeByName("D2:E{0}".format(result_len)) oRange.ParaAdjust = RIGHT standard_style = doc.StyleFamilies.getByName("PageStyles").getByName( "Standard") standard_style.setPropertyValue("FooterIsOn", True) f_text = standard_style.getPropertyValue("FooterText") f_cursor = f_text.createTextCursor() f_cursor.setPropertyValue("CharHeight", 8) f_text.insertString(f_cursor, "Προγραμματισμός: Κώνστας Μαρματάκης", 0) f_text.insertControlCharacter(f_cursor, PARAGRAPH_BREAK, 0) d = datetime.date.today() f_text.insertString(f_cursor, d.strftime("%d-%m-%Y"), 0) f_text.insertControlCharacter(f_cursor, PARAGRAPH_BREAK, 0) oField = doc.createInstance("com.sun.star.text.TextField.PageNumber") oField.NumberingType = 4 # Arabic f_text.insertTextContent(f_cursor, oField, 0) f_cursor.ParaAdjust = 1
def addImageToWriterDocument(self, doc, cursor, url): text = doc.Text oShape = doc.createInstance("com.sun.star.text.GraphicObject") oShape.GraphicURL = url text.insertTextContent(cursor, oShape, uno.Bool(1))