7 from config.odt
import lmf_to_odt
8 from utils.error_handling
import OutputError
9 from utils.io
import open_read
11 from odf.opendocument
import OpenDocumentText
12 from odf.style
import Style, TextProperties
13 from odf.text
import H, P, Span
16 """! @brief Read file contents.
17 @param filename The name of the file with full path containing information to read, for instance the text introduction of the document: 'user/config/introduction.txt'.
18 @return A Python string containing read information.
21 if filename
is not None:
23 contents = file.read()
27 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):
28 """! @brief Write a document file.
29 @param object The LMF instance to convert into document output format.
30 @param filename The name of the document file to write with full path, for instance 'user/output.odt'.
31 @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.
32 @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.
33 @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.
34 @param sort_order Python list. Default value is 'None', which means that the document output is alphabetically ordered.
35 @param paradigms A boolean value to introduce paradigms in document or not.
36 @param reverse A boolean value to set if a reverse dictionary is wanted.
39 if sort_order
is None:
41 sort_order = dict([(c, ord(c))
for c
in string.lowercase])
42 up = dict([(c, ord(c) + 32)
for c
in string.uppercase])
44 sort_order.update({
'':0,
' ':0})
45 textdoc = OpenDocumentText()
48 h1style = Style(name=
"Heading 1", family=
"paragraph")
49 h1style.addElement(TextProperties(attributes={
'fontsize':
"24pt",
'fontweight':
"bold" }))
52 boldstyle = Style(name=
"Bold", family=
"text")
53 boldprop = TextProperties(fontweight=
"bold", fontname=
"Arial", fontsize=
"8pt")
54 boldstyle.addElement(boldprop)
55 textdoc.automaticstyles.addElement(boldstyle)
57 if object.__class__.__name__ ==
"LexicalResource":
58 for lexicon
in object.get_lexicons():
60 h = H(outlinelevel=1, stylename=h1style, text=lexicon.get_id())
61 textdoc.text.addElement(h)
63 p = P(text=lexicon.get_label())
65 boldpart = Span(stylename=boldstyle, text=
"Test. ")
66 p.addElement(boldpart)
68 if introduction
is not None:
70 textdoc.text.addElement(p)
74 lmf2odt(lexicon, textdoc, items, sort_order, paradigms, reverse)
76 raise OutputError(object,
"Object to write must be a Lexical Resource.")
77 textdoc.save(filename)
def file_read
Read file contents.
def open_read
Open file in read mode (automatically decode file in unicode).
def odt_write
Write a document file.