This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write2odt.py
63 lines (47 loc) · 1.87 KB
/
write2odt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
import re
from odf.opendocument import OpenDocumentText
from odf import teletype
from odf.text import P
from odf.text import Ruby
from odf.text import RubyBase
from odf.text import RubyText
TOKENS_KANJI = re.compile(u'[一-龠]+') # kanji
def add_str(paragraph_element, string):
"""Add string to paragraph element."""
teletype.addTextToElement(paragraph_element, string)
def add_rubi(paragraph_element, str_base, str_rubi):
"""Add rubi to paragraph element."""
rubi_element = Ruby(stylename="Rubi1")
rubi_element.addElement(RubyBase(text=str_base))
rubi_element.addElement(RubyText(text=str_rubi))
paragraph_element.addElement(rubi_element)
def create_paragraph(string):
"""Create a paragraph element with rubi text."""
paragraph_element = P()
while 1:
m = re.search(TOKENS_KANJI, string) # detect kanji start position
if m is not None: # if first part of the string is non-kanji
add_str(paragraph_element, string[:m.start()])
string = string[m.start():] # truncate non-kanji
m = re.match(r'(.*?)\((.*?)\)(.*)', string)
if m is None:
add_str(paragraph_element, string)
break
else:
add_rubi(paragraph_element, m.group(1), m.group(2))
string = m.group(3)
return paragraph_element
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)