def _make_layout(self, doc, layout, firstpage): # set paper dimensions pageLayout = style.PageLayout(name=u"pl1") doc.automaticstyles.addElement(pageLayout) plProp = style.PageLayoutProperties(pageheight=str(layout.height), pagewidth=str(layout.width), marginleft=str(layout.left), marginright=str(layout.right), margintop=str(layout.top), marginbottom=str(layout.bottom)) pageLayout.addElement(plProp) # add page numbers to the footers footer = style.Footer() foostyle = style.Style(name="Footer", family="paragraph") foostyle.addElement(style.ParagraphProperties(textalign='center')) foostyle.addElement(style.TextProperties(fontsize='10pt')) doc.automaticstyles.addElement(foostyle) p = text.P(stylename=foostyle) p.addElement( text.PageNumber(selectpage="current", pageadjust=str(firstpage - 1))) footer.addElement(p) masterpage = style.MasterPage(name=u"Standard", pagelayoutname=pageLayout) masterpage.addElement(footer) doc.masterstyles.addElement(masterpage)
def testTable(self): """ Create a presentation with a page layout called MyLayout """ presdoc = OpenDocumentPresentation() # We must describe the dimensions of the page pagelayout = style.PageLayout(name="MyLayout") presdoc.automaticstyles.addElement(pagelayout) pagelayout.addElement( style.PageLayoutProperties(margin="0cm", pagewidth="28cm", pageheight="21cm", printorientation="landscape")) # Every drawing page must have a master page assigned to it. masterpage = style.MasterPage(name="MyMaster", pagelayoutname=pagelayout) presdoc.masterstyles.addElement(masterpage) # Style for the title frame of the page # We set a centered 34pt font with yellowish background titlestyle = style.Style(name="MyMaster-title", family="presentation") titlestyle.addElement(style.ParagraphProperties(textalign="center")) titlestyle.addElement(style.TextProperties(fontsize="34pt")) titlestyle.addElement(style.GraphicProperties(fillcolor="#ffff99")) presdoc.styles.addElement(titlestyle) # Style for the photo frame mainstyle = style.Style(name="MyMaster-main", family="presentation") presdoc.styles.addElement(mainstyle) # Create style for drawing page dpstyle = style.Style(name="dp1", family="drawing-page") presdoc.automaticstyles.addElement(dpstyle) page = draw.Page(stylename=dpstyle, masterpagename=masterpage) presdoc.presentation.addElement(page) titleframe = draw.Frame(stylename=titlestyle, width="720pt", height="56pt", x="40pt", y="10pt") page.addElement(titleframe) textbox = draw.TextBox() titleframe.addElement(textbox) textbox.addElement(text.P(text="Presentation")) mainframe = draw.Frame(stylename=mainstyle, width="720pt", height="500pt", x="0pt", y="56pt") page.addElement(mainframe) mainframe.addElement(table.Table())
def testStyle(self): """ Create a presentation with a page layout called MyLayout Add a presentation style for the title Check that MyLayout is listed in styles.xml """ presdoc = OpenDocumentPresentation() # We must describe the dimensions of the page pagelayout = style.PageLayout(name="MyLayout") presdoc.automaticstyles.addElement(pagelayout) pagelayout.addElement( style.PageLayoutProperties(margin="0cm", pagewidth="28cm", pageheight="21cm", printorientation="landscape")) # Every drawing page must have a master page assigned to it. masterpage = style.MasterPage(name="MyMaster", pagelayoutname=pagelayout) presdoc.masterstyles.addElement(masterpage) # Style for the title frame of the page # We set a centered 34pt font with yellowish background titlestyle = style.Style(name="MyMaster-title", family="presentation") titlestyle.addElement(style.ParagraphProperties(textalign="center")) titlestyle.addElement(style.TextProperties(fontsize="34pt")) titlestyle.addElement(style.GraphicProperties(fillcolor="#ffff99")) presdoc.styles.addElement(titlestyle) s = presdoc.stylesxml() self.assertContains( s, '<style:page-layout style:name="MyLayout"><style:page-layout-properties ' ) e = ElementParser(s, 'style:page-layout-properties') self.assertEqual(e.element, 'style:page-layout-properties') self.assertTrue(e.has_value("fo:margin", "0cm")) self.assertTrue(e.has_value("fo:page-width", "28cm")) self.assertTrue(e.has_value("fo:page-height", "21cm")) self.assertTrue(e.has_value("style:print-orientation", "landscape")) e = ElementParser(s, 'style:style') self.assertTrue(e.has_value("style:name", "MyMaster-title")) self.assertTrue(e.has_value("style:display-name", "MyMaster-title")) self.assertTrue(e.has_value("style:family", "presentation")) self.assertContains( s, '<style:paragraph-properties fo:text-align="center"/><style:text-properties fo:font-size="34pt"/><style:graphic-properties draw:fill-color="#ffff99"/></style:style></office:styles>' ) e = ElementParser(s, 'style:master-page') self.assertTrue(e.has_value("style:name", "MyMaster")) self.assertTrue(e.has_value("style:display-name", "MyMaster")) self.assertTrue(e.has_value("style:page-layout-name", "MyLayout"))
def testMasterWithHeader(self): """ Create a text document with a page layout called "pagelayout" Add a master page Check that pagelayout is listed in styles.xml """ textdoc = OpenDocumentText() pl = style.PageLayout(name="pagelayout") textdoc.automaticstyles.addElement(pl) mp = style.MasterPage(name="Standard", pagelayoutname=pl) textdoc.masterstyles.addElement(mp) h = style.Header() hp = text.P(text="header try") h.addElement(hp) mp.addElement(h) s = unicode(textdoc.stylesxml(),'UTF-8') self.assertContains(s, u'<office:automatic-styles><style:page-layout style:name="pagelayout"/></office:automatic-styles>')
def _write_journal(self, journal): path = os.path.join( options.REPORTS_DIR, 'Журнал {} {} {}.odt'.format( self.year, self.months[self.month], self.doctor[1], )) doc = opendocument.OpenDocumentText() table_style = style.Style(name='tableStyle', family='table', masterpagename='masterQ') page_layout = style.PageLayout(name='pageStyle') page_layout.addElement( style.PageLayoutProperties( printorientation='landscape', margin='0cm', pagewidth='290cm', pageheight='21cm', )) master_q = style.MasterPage(name='masterQ', pagelayoutname=page_layout) doc.automaticstyles.addElement(table_style) doc.automaticstyles.addElement(page_layout) doc.masterstyles.addElement(master_q) journal = ([['Дата', 'ФИО', 'Дата Рождения', 'Адрес', 'Заключение']] + journal) _table = table.Table(name='journal') _table.addElement( table.TableColumn(numbercolumnsrepeated=len(journal[0]))) for data in journal: row = table.TableRow() _table.addElement(row) for col in data: cell = table.TableCell(valuetype='string') cell.addElement(text.P(text=str(col or ''))) row.addElement(cell) doc.text.addElement(_table) try: doc.save(path) except PermissionError: self.main_window.create_alert( 'Файл журнала используется.\nЗакройте его и создайте снова.') return self._open(path)
def testAutomaticStyles(self): """ Create a text document with a page layout called "pagelayout" Add a master page Add an automatic style for the heading Check that pagelayout is listed in styles.xml under automatic-styles Check that the heading style is NOT listed in styles.xml Check that the pagelayout is NOT listed in content.xml """ textdoc = OpenDocumentText() parastyle = style.Style(name="Para", family="paragraph") parastyle.addElement( style.ParagraphProperties(numberlines="false", linenumber="0")) parastyle.addElement( style.TextProperties(fontsize="24pt", fontweight="bold")) textdoc.automaticstyles.addElement(parastyle) hpstyle = style.Style(name="HeaderPara", family="paragraph") hpstyle.addElement(style.ParagraphProperties(linenumber="0")) hpstyle.addElement( style.TextProperties(fontsize="18pt", fontstyle="italic")) textdoc.automaticstyles.addElement(hpstyle) pl = style.PageLayout(name="pagelayout") textdoc.automaticstyles.addElement(pl) mp = style.MasterPage(name="Standard", pagelayoutname=pl) textdoc.masterstyles.addElement(mp) h = style.Header() hp = text.P(text="header content", stylename=hpstyle) h.addElement(hp) mp.addElement(h) textdoc.text.addElement(text.P(text="Paragraph 1", stylename=parastyle)) # Check styles.xml s = textdoc.stylesxml() self.assertContains(s, u'<style:page-layout style:name="pagelayout"/>') self.assertContains(s, u'style:name="HeaderPara"') self.assertNotContains(s, u'style:name="Para" ') # Check content.xml s = textdoc.contentxml() # contentxml is supposed to yed a byts self.assertNotContains( s, b'<style:page-layout style:name="pagelayout"/>') self.assertContains(s, b'style:name="Para"')