7 from config.doc
import lmf_to_doc
8 from utils.error_handling
import OutputError
9 from utils.io
import open_read
11 from docx
import Document
12 from docx.shared
import Cm
15 """! @brief Read file contents.
16 @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'.
17 @return A Python string containing read information.
20 if filename
is not None:
22 contents = file.read()
26 def doc_write(object, filename, introduction=None, lmf2doc=lmf_to_doc, items=lambda lexical_entry: lexical_entry.get_lexeme(), sort_order=
None, paradigms=
False, reverse=
False):
27 """! @brief Write a document file.
28 @param object The LMF instance to convert into document output format.
29 @param filename The name of the document file to write with full path, for instance 'user/output.doc'.
30 @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.
31 @param lmf2doc A function giving the mapping from LMF representation information that must be written to docx commands, in a defined order. Default value is 'lmf_to_doc' function defined in 'pylmflib/config/doc.py'. Please refer to it as an example.
32 @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.
33 @param sort_order Python list. Default value is 'None', which means that the document output is alphabetically ordered.
34 @param paradigms A boolean value to introduce paradigms in document or not.
35 @param reverse A boolean value to set if a reverse dictionary is wanted.
38 if sort_order
is None:
40 sort_order = dict([(c, ord(c))
for c
in string.lowercase])
41 up = dict([(c, ord(c) + 32)
for c
in string.uppercase])
43 sort_order.update({
'':0,
' ':0})
46 section = document.sections[0]
47 section.top_margin = Cm(1)
48 section.bottom_margin = Cm(1)
49 section.left_margin = Cm(1)
50 section.right_margin = Cm(1)
52 if object.__class__.__name__ ==
"LexicalResource":
53 for lexicon
in object.get_lexicons():
55 document.add_heading(lexicon.get_id(), 0)
57 document.add_paragraph(lexicon.get_label())
59 document.add_page_break()
61 if introduction
is not None:
62 document.add_paragraph(
file_read(introduction))
64 lmf2doc(lexicon, document, items, sort_order, paradigms, reverse)
66 raise OutputError(object,
"Object to write must be a Lexical Resource.")
67 document.save(filename)
def open_read
Open file in read mode (automatically decode file in unicode).
def doc_write
Write a document file.
def file_read
Read file contents.