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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| #!/usr/bin/env python
"""Create basic text styles.
"""
import os
from pathlib import Path
from odfdo import Document, Header, Paragraph, Style
_DOC_SEQUENCE = 330
OUTPUT_DIR = Path(__file__).parent / "recipes_output" / "basic_styles"
TARGET = "document.odt"
def save_new(document: Document, name: str) -> None:
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
new_path = OUTPUT_DIR / name
print("Saving:", new_path)
document.save(new_path, pretty=True)
def _create_style_header_blue(document: Document) -> None:
style = Style(
family="paragraph",
name="header_blue",
display_name="header_blue",
parent_style="Heading",
area="text",
bold=True,
color="blue",
size="160%",
)
style.set_attribute("style:default-outline-level", "1")
document.insert_style(style)
def _create_style_header_navy(document: Document) -> None:
style = Style(
family="paragraph",
name="header_navy",
display_name="header_navy",
parent_style="Heading",
area="text",
bold=True,
color="navy",
size="120%",
)
style.set_attribute("style:default-outline-level", "2")
document.insert_style(style)
def _create_style_steel(document: Document) -> None:
style = Style(
family="paragraph",
area="text",
name="steel",
display_name="steel",
color="yellow",
background_color="darkblue",
)
style.set_properties(
area="graphic",
properties={
"draw:fill": "solid",
"draw:fill-color": "darkblue",
},
)
document.insert_style(style)
def _create_style_special(document: Document) -> None:
style = Style(
family="paragraph",
area="text",
name="special",
display_name="special",
font="Courier New",
font_family="Courier New",
font_style_name="Regular",
font_pitch="fixed",
background_color="AntiqueWhite",
)
style.set_properties(
area="paragraph",
properties={
"fo:margin-left": "2cm",
"fo:margin-right": "2cm",
"fo:line-height": "150%",
"fo:text-align": "center",
},
)
document.insert_style(style)
def _create_style_bold_gold(document: Document) -> None:
style = Style(
family="text",
name="bold_gold",
display_name="bold_gold",
bold=True,
color="darkgoldenrod",
)
document.insert_style(style)
def _create_style_italic_lime(document: Document) -> None:
style = Style(
family="text",
name="italic_lime",
display_name="italic_lime",
italic=True,
size="120%",
color="lime",
)
document.insert_style(style)
def add_styles(document: Document) -> None:
_create_style_header_blue(document)
_create_style_header_navy(document)
_create_style_steel(document)
_create_style_special(document)
_create_style_bold_gold(document)
_create_style_italic_lime(document)
def add_content(document: Document) -> None:
body = document.body
body.append(Header(1, "First level header", style="header_blue"))
body.append(Header(2, "First sub header", style="header_navy"))
para = Paragraph(
"Lorem ipsum dolor sit amet, consectetuer "
"adipiscing elit. Sed non risus. "
"Suspendisse lectus tortor, dignissim sit amet, "
"adipiscing nec, ultricies sed, dolor."
)
para.set_span("bold_gold", regex="dolor")
para.set_span("italic_lime", regex=r"\w+ing")
body.append(para)
body.append(Header(2, "Second sub header", style="header_navy"))
para = Paragraph(
"Cras elementum ultrices diam. Maecenas ligula massa, "
"varius a, semper congue, euismod non, mi. Proin porttitor, "
"orci nec nonummy molestie, enim est eleifend mi, non "
"fermentum diam nisl sit amet erat. Duis semper.",
style="steel",
)
para.set_span("italic_lime", regex="semper")
body.append(para)
body.append(Header(2, "Third sub header", style="header_navy"))
para = Paragraph(
"Duis arcu massa, scelerisque vitae, consequat in, pretium a, "
"enim. Pellentesque congue. Ut in risus volutpat libero "
"pharetra tempor. Cras vestibulum bibendum augue. Praesent "
"egestas leo in pede. Praesent blandit odio eu enim. "
"Pellentesque sed dui ut augue blandit sodales.",
style="special",
)
body.append(para)
def create_document() -> Document:
document = Document()
body = document.body
body.clear()
add_styles(document)
add_content(document)
return document
def main() -> None:
document = create_document()
test_unit(document)
save_new(document, TARGET)
def test_unit(document: Document) -> None:
# only for test suite:
if "ODFDO_TESTING" not in os.environ:
return
style1 = document.get_style("paragraph", "header_blue").serialize()
assert 'name="header_blue"' in style1
assert 'color="#0000FF"' in style1
assert 'font-weight="bold"' in style1
assert 'font-size="160%"' in style1
style2 = document.get_style("paragraph", "header_navy").serialize()
assert 'name="header_navy"' in style2
assert 'color="#000080"' in style2
assert 'font-weight="bold"' in style2
assert 'font-size="120%"' in style2
style3 = document.get_style("paragraph", "steel").serialize()
assert 'name="steel"' in style3
assert 'color="#FFFF00"' in style3
assert "graphic-properties" in style3
assert 'draw:fill-color="#00008B"' in style3
style4 = document.get_style("paragraph", "special").serialize()
assert 'name="special"' in style4
assert 'background-color="#FAEBD7"' in style4
assert "Courier" in style4
assert 'line-height="150%"' in style4
assert 'margin-left="2cm"' in style4
assert 'margin-right="2cm"' in style4
assert 'text-align="center"' in style4
style5 = document.get_style("text", "bold_gold").serialize()
assert 'name="bold_gold"' in style5
assert 'color="#B8860B"' in style5
assert 'font-weight="bold"' in style5
style6 = document.get_style("text", "italic_lime").serialize()
assert 'name="italic_lime"' in style6
assert 'color="#00FF00"' in style6
assert 'font-style="italic"' in style6
assert 'font-size="120%"' in style6
if __name__ == "__main__":
main() |
Partager