本文整理汇总了Python中odf.opendocument.OpenDocumentText.save方法的典型用法代码示例。如果您正苦于以下问题:Python OpenDocumentText.save方法的具体用法?Python OpenDocumentText.save怎么用?Python OpenDocumentText.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类odf.opendocument.OpenDocumentText
的用法示例。
在下文中一共展示了OpenDocumentText.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_headings
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def test_headings(self):
textdoc = OpenDocumentText()
textdoc.text.addElement(H(outlinelevel=1, text="Heading 1"))
textdoc.text.addElement(P(text="Hello World!"))
textdoc.text.addElement(H(outlinelevel=2, text="Heading 2"))
textdoc.save("TEST.odt")
self.saved = True
result = odf2moinmoin.ODF2MoinMoin("TEST.odt")
self.assertEquals(u'= Heading 1 =\n\nHello World!\n== Heading 2 ==\n\n', result.toString())
示例2: test_linebreak
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def test_linebreak(self):
textdoc = OpenDocumentText()
p = P(text="Hello World!")
textdoc.text.addElement(p)
p.addElement(LineBreak())
p.addText("Line 2")
textdoc.save("TEST.odt")
self.saved = True
result = odf2moinmoin.ODF2MoinMoin("TEST.odt")
self.assertEquals(u'Hello World![[BR]]Line 2\n', result.toString())
示例3: exportODT
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def exportODT(examen, archivo):
"""
Function to export the data exam to a odt file.
The input data is the exam and the ODT file to write.
This function uses odfpy library
"""
# Extract data from exam
asignatura = examen.asignatura
nombre = examen.nombre
preguntas = examen.preguntas
textdoc = OpenDocumentText()
h = H(outlinelevel=1, text=asignatura)
textdoc.text.addElement(h)
h = H(outlinelevel=4, text=nombre)
textdoc.text.addElement(h)
# an element is added to the object "textdoc" for each question
i = 1
for pregunta in preguntas:
texto = str(i) + ".- " + pregunta.texto
p = P(text = texto)
textdoc.text.addElement(p)
# For test questions
if pregunta.tipo == 1:
for opcion in pregunta.opciones:
texto = opcion.letra + ") " + opcion.texto
p = P(text = texto)
textdoc.text.addElement(p)
# For true or false questions
elif pregunta.tipo == 2:
texto = "A) Verdadero"
p = P(text = texto.encode('utf-8'))
textdoc.text.addElement(p)
texto = "B) Falso"
p = P(text = texto)
textdoc.text.addElement(p)
p = P()
textdoc.text.addElement(p)
p = P()
textdoc.text.addElement(p)
i = i + 1
# Save complete file
textdoc.save(archivo)
return examen
示例4: Document_Generic
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
class Document_Generic(object):
"""Example of document"""
def __init__(self):
self.document = OpenDocumentText()
self.defineStyles()
def defineStyles(self):
""" """
pass
def addParagraphStyle(self, id, name, paragraph_properties={}, text_properties={}):
""" """
style = Style(name=name, family="paragraph")
if len(paragraph_properties) > 0:
style.addElement(ParagraphProperties(**paragraph_properties))
if len(text_properties) > 0:
style.addElement(TextProperties(**text_properties))
setattr(self, id, style)
self.document.styles.addElement(style)
def addTableColumnStyle(self, id, name, properties={}):
""" """
style = Style(name=name, family="table-column")
style.addElement(TableColumnProperties(**properties))
setattr(self, id, style)
self.document.automaticstyles.addElement(style)
def addParagraph(self, text, stylename):
""" """
stylename = getattr(self, stylename, None)
p = P(stylename=stylename, text=text)
self.document.text.addElement(p)
def addTable(self, content, cell_style, column_styles=[]):
""" """
cell_style = getattr(self, cell_style, None)
table = Table()
for style in column_styles:
if "stylename" in style.keys():
style["stylename"] = getattr(self, style["stylename"], None)
table.addElement(TableColumn(**style))
for row in content:
tr = TableRow()
table.addElement(tr)
for cell in row:
tc = TableCell()
tr.addElement(tc)
p = P(stylename=cell_style,text=cell)
tc.addElement(p)
self.document.text.addElement(table)
def save(self, filename):
""" """
self.document.save(filename)
示例5: test_headings
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def test_headings(self):
""" Create a document, save it and load it """
textdoc = OpenDocumentText()
textdoc.text.addElement(H(outlinelevel=1, text=u"Heading 1"))
textdoc.text.addElement(P(text=u"Hello World!"))
textdoc.text.addElement(H(outlinelevel=2, text=u"Heading 2"))
textdoc.save(u"TEST.odt")
self.saved = True
d = load(u"TEST.odt")
result = d.contentxml() # contentxml() is supposed to yeld a bytes
self.assertNotEqual(-1, result.find(b"""<text:h text:outline-level="1">Heading 1</text:h><text:p>Hello World!</text:p><text:h text:outline-level="2">Heading 2</text:h>"""))
示例6: TestUnicode
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
class TestUnicode(unittest.TestCase):
def setUp(self):
self.textdoc = OpenDocumentText()
self.saved = False
def tearDown(self):
if self.saved:
os.unlink("TEST.odt")
def test_subobject(self):
df = draw.Frame(width="476pt", height="404pt", anchortype="paragraph")
self.textdoc.text.addElement(df)
subdoc = OpenDocumentText()
# Here we add the subdocument to the main document. We get back a reference
# to use in the href.
subloc = self.textdoc.addObject(subdoc)
self.assertEqual(subloc, "./Object 1")
do = draw.Object(href=subloc)
df.addElement(do)
subsubdoc = OpenDocumentText()
subsubloc = subdoc.addObject(subsubdoc)
self.assertEqual(subsubloc, "./Object 1/Object 1")
c = unicode(self.textdoc.contentxml(), "UTF-8")
c.index(
u'<office:body><office:text><draw:frame svg:width="476pt" text:anchor-type="paragraph" svg:height="404pt"><draw:object xlink:href="./Object 1"/></draw:frame></office:text></office:body>'
)
c.index(u'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
self.textdoc.save("TEST.odt")
self.saved = True
m = _getxmlpart("TEST.odt", "META-INF/manifest.xml")
m.index(
'<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"/>'
)
m.index('<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/>')
m.index('<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>')
m.index('<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/>')
m.index(
'<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="Object 1/"/>'
)
m.index('<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="Object 1/styles.xml"/>')
m.index('<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="Object 1/content.xml"/>')
m.index(
'<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="Object 1/Object 1/"/>'
)
m.index(
'<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="Object 1/Object 1/styles.xml"/>'
)
m.index(
'<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="Object 1/Object 1/content.xml"/>'
)
示例7: test_linebreak
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def test_linebreak(self):
""" Test that a line break (empty) element show correctly """
textdoc = OpenDocumentText()
p = P(text=u"Hello World!")
textdoc.text.addElement(p)
p.addElement(LineBreak())
p.addText(u"Line 2")
textdoc.save(u"TEST.odt")
self.saved = True
d = load(u"TEST.odt")
result = d.contentxml() # contentxml() is supposed to yeld a bytes
self.assertNotEqual(-1, result.find(b"""<text:p>Hello World!<text:line-break/>Line 2</text:p>"""))
示例8: TestUnicode
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
class TestUnicode(unittest.TestCase):
def setUp(self):
self.textdoc = OpenDocumentText()
self.saved = False
def tearDown(self):
if self.saved:
os.unlink("TEST.odt")
def assertContains(self, stack, needle):
self.assertNotEqual(-1, stack.find(needle))
def assertNotContains(self, stack, needle):
self.assertEqual(-1, stack.find(needle))
@unittest.skipIf(sys.version_info[0] != 2,
"For Python3, unicode strings are type 'str'.")
def test_xstyle(self):
self.assertRaises(UnicodeDecodeError, style.Style, name="X✗", family="paragraph")
xstyle = style.Style(name=u"X✗", family=u"paragraph")
pp = style.ParagraphProperties(padding=u"0.2cm")
pp.setAttribute(u"backgroundcolor", u"rød")
xstyle.addElement(pp)
self.textdoc.styles.addElement(xstyle)
self.textdoc.save(u"TEST.odt")
self.saved = True
def test_text(self):
p = P(text=u"Æblegrød")
p.addText(u' Blåbærgrød')
self.textdoc.text.addElement(p)
self.textdoc.save(u"TEST.odt")
self.saved = True
def test_contenttext(self):
p = H(outlinelevel=1,text=u"Æblegrød")
p.addText(u' Blåbærgrød')
self.textdoc.text.addElement(p)
c = self.textdoc.contentxml() # contentxml is supposed to yield a bytes
self.assertContains(c, b'<office:body><office:text><text:h text:outline-level="1">\xc3\x86blegr\xc3\xb8d Bl\xc3\xa5b\xc3\xa6rgr\xc3\xb8d</text:h></office:text></office:body>')
self.assertContains(c, b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
self.assertContains(c, b'<office:automatic-styles/>')
def test_illegaltext(self):
p = H(outlinelevel=1,text=u"Spot \u001e the")
p.addText(u' d\u00a3libe\u0000rate \ud801 mistakes\U0002fffe')
self.textdoc.text.addElement(p)
c = self.textdoc.contentxml() # contentxml is supposed to yield a bytes
# unicode replacement char \UFFFD === \xef\xbf\xbd in UTF-8
self.assertContains(c, b'<office:body><office:text><text:h text:outline-level="1">Spot \xef\xbf\xbd the d\xc2\xa3libe\xef\xbf\xbdrate \xef\xbf\xbd mistakes\xef\xbf\xbd</text:h></office:text></office:body>')
self.assertContains(c, b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
self.assertContains(c, b'<office:automatic-styles/>')
示例9: odt_write
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def odt_write(object, filename, introduction=None, lmf2odt=lmf_to_odt, items=lambda lexical_entry: lexical_entry.get_lexeme(), sort_order=None, paradigms=False, reverse=False):
"""! @brief Write a document file.
@param object The LMF instance to convert into document output format.
@param filename The name of the document file to write with full path, for instance 'user/output.odt'.
@param introduction The name of the text file with full path containing the introduction of the document, for instance 'user/config/introduction.txt'. Default value is None.
@param lmf2odt A function giving the mapping from LMF representation information that must be written to ODT commands, in a defined order. Default value is 'lmf_to_odt' function defined in 'pylmflib/config/odt.py'. Please refer to it as an example.
@param items Lambda function giving the item to sort. Default value is 'lambda lexical_entry: lexical_entry.get_lexeme()', which means that the items to sort are lexemes.
@param sort_order Python list. Default value is 'None', which means that the document output is alphabetically ordered.
@param paradigms A boolean value to introduce paradigms in document or not.
@param reverse A boolean value to set if a reverse dictionary is wanted.
"""
import string
if sort_order is None:
# Lowercase and uppercase letters must have the same rank
sort_order = dict([(c, ord(c)) for c in string.lowercase])
up = dict([(c, ord(c) + 32) for c in string.uppercase])
sort_order.update(up)
sort_order.update({'':0, ' ':0})
textdoc = OpenDocumentText()
# Styles
s = textdoc.styles
h1style = Style(name="Heading 1", family="paragraph")
h1style.addElement(TextProperties(attributes={'fontsize':"24pt", 'fontweight':"bold" }))
s.addElement(h1style)
# An automatic style
boldstyle = Style(name="Bold", family="text")
boldprop = TextProperties(fontweight="bold", fontname="Arial", fontsize="8pt")
boldstyle.addElement(boldprop)
textdoc.automaticstyles.addElement(boldstyle)
# Parse LMF values
if object.__class__.__name__ == "LexicalResource":
for lexicon in object.get_lexicons():
# Document title
h = H(outlinelevel=1, stylename=h1style, text=lexicon.get_id())
textdoc.text.addElement(h)
# Plain paragraph
p = P(text=lexicon.get_label())
# Text
boldpart = Span(stylename=boldstyle, text="Test. ")
p.addElement(boldpart)
# Introduction
if introduction is not None:
p.addText(file_read(introduction))
textdoc.text.addElement(p)
# Page break
#
# Text body
lmf2odt(lexicon, textdoc, items, sort_order, paradigms, reverse)
else:
raise OutputError(object, "Object to write must be a Lexical Resource.")
textdoc.save(filename)
示例10: TestUnicode
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
class TestUnicode(unittest.TestCase):
def setUp(self):
self.textdoc = OpenDocumentText()
self.saved = False
def tearDown(self):
if self.saved:
os.unlink("TEST.odt")
def test_subobject(self):
df = draw.Frame(width="476pt", height="404pt", anchortype="paragraph")
self.textdoc.text.addElement(df)
subdoc = OpenDocumentText()
# Here we add the subdocument to the main document. We get back a reference
# to use in the href.
subloc = self.textdoc.addObject(subdoc)
self.assertEqual(subloc,'./Object 1')
do = draw.Object(href=subloc)
df.addElement(do)
subsubdoc = OpenDocumentText()
subsubloc = subdoc.addObject(subsubdoc)
self.assertEqual(subsubloc,'./Object 1/Object 1')
c = self.textdoc.contentxml() # contentxml() is supposed to yeld a bytes
c.index(b'<office:body><office:text><draw:frame ')
e = ElementParser(c.decode("utf-8"), u'draw:frame')
# e = ElementParser('<draw:frame svg:width="476pt" text:anchor-type="paragraph" svg:height="404pt">')
self.assertTrue(e.has_value(u'svg:width',"476pt"))
self.assertTrue(e.has_value(u'svg:height',"404pt"))
self.assertTrue(e.has_value(u'text:anchor-type',"paragraph"))
self.assertFalse(e.has_value(u'svg:height',"476pt"))
c.index(b'<draw:object xlink:href="./Object 1"/></draw:frame></office:text></office:body>')
c.index(b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
self.textdoc.save(u"TEST.odt")
self.saved = True
m = _getxmlpart(u"TEST.odt", u"META-INF/manifest.xml").decode('utf-8')
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="content.xml"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="meta.xml"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="Object 1/"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/styles.xml"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/content.xml"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="Object 1/Object 1/"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/Object 1/styles.xml"'))
assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/Object 1/content.xml"'))
示例11: convert_and_save
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def convert_and_save(text, filename):
textdoc = OpenDocumentText()
# Useless: This style setting is somehow overwritten by LibreOffice
'''
rubistyle = Style(name="Rubi1", family="ruby")
rubistyle.addElement(RubyProperties(attributes={"rubyalign": "center", "rubyposition": "above"}))
textdoc.styles.addElement(rubistyle)
'''
lines = text.splitlines()
for line in lines:
paragraph_element = create_paragraph(line)
textdoc.text.addElement(paragraph_element)
textdoc.save(filename)
示例12: image_spice
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def image_spice(genre, font, spice):
for myspice in spice:
myodt = OpenDocumentText()
# template is updated by font
text_template[genre].update(text_style[genre][font])
paragraph_template[genre].update(paragraph_style[genre][font])
template = genre2template(genre,
text_template[genre],
font,
paragraph_template[genre])
myodt.styles.addElement(template)
image_spices(myodt, myspice, font)
text_frame(myodt, genre, font)
label(myodt, myspice, font)
myname = genre + "_" + font + "_" + myspice
myodt.save(myname, True)
示例13: TestUnicode
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
class TestUnicode(unittest.TestCase):
def setUp(self):
self.textdoc = OpenDocumentText()
self.saved = False
def tearDown(self):
if self.saved:
os.unlink("TEST.odt")
def assertContains(self, stack, needle):
self.assertNotEqual(-1, stack.find(needle))
def assertNotContains(self, stack, needle):
self.assertEquals(-1, stack.find(needle))
def test_xstyle(self):
self.assertRaises(UnicodeDecodeError, style.Style, name="X✗", family="paragraph")
xstyle = style.Style(name=u"X✗", family=u"paragraph")
pp = style.ParagraphProperties(padding=u"0.2cm")
pp.setAttribute(u"backgroundcolor", u"rød")
xstyle.addElement(pp)
self.textdoc.styles.addElement(xstyle)
self.textdoc.save(u"TEST.odt")
self.saved = True
def test_text(self):
p = P(text=u"Æblegrød")
p.addText(u' Blåbærgrød')
self.textdoc.text.addElement(p)
self.textdoc.save(u"TEST.odt")
self.saved = True
def test_contenttext(self):
p = H(outlinelevel=1,text=u"Æblegrød")
p.addText(u' Blåbærgrød')
self.textdoc.text.addElement(p)
c = self.textdoc.contentxml() # contentxml is supposed to yeld a bytes
self.assertContains(c, b'<office:body><office:text><text:h text:outline-level="1">\xc3\x86blegr\xc3\xb8d Bl\xc3\xa5b\xc3\xa6rgr\xc3\xb8d</text:h></office:text></office:body>')
self.assertContains(c, b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
self.assertContains(c, b'<office:automatic-styles/>')
示例14: setUp
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def setUp(self):
d = OpenDocumentText()
# Styles
h1style = style.Style(name=u"Heading 1",family=u"paragraph")
h1style.addElement(style.TextProperties(attributes={'fontsize':u"24pt", 'fontweight':u"bold"}))
d.styles.addElement(h1style)
boldstyle = style.Style(name=u"Bold",family=u"text")
boldstyle.addElement(style.TextProperties(attributes={'fontweight':u"bold"}))
d.automaticstyles.addElement(boldstyle)
# Text
h = H(outlinelevel=1, stylename=h1style, text=u"Simple test document")
d.text.addElement(h)
p = P(text=u"The earth's climate has not changed many times in the course of its long history. ")
d.text.addElement(p)
boldpart = Span(stylename=boldstyle, text=u"This part is bold. ")
p.addElement(boldpart)
p.addText(u"This is after bold.")
d.save(u"TEST.odt")
示例15: parse
# 需要导入模块: from odf.opendocument import OpenDocumentText [as 别名]
# 或者: from odf.opendocument.OpenDocumentText import save [as 别名]
def parse():
file = open("in.srt", "r")
skip = False;
statement = False;
cur_text = ""
doc = OpenDocumentText()
for line in file:
line = line.strip('\n').strip('\n').strip(' ')
if skip:
skip = False
statement = True
continue
if len(line) == 0:
p = P(text = cur_text)
p1 = P(text = " ")
doc.text.addElement(p)
doc.text.addElement(p1)
cur_text = ""
statement = False
continue
lst = re.findall(r'\d+', line)
if (len(lst) > 0):
skip = True
continue
if statement == True:
# Appending a line to text
cur_text = cur_text + " " + line
continue
doc.save("out.odt")