Write Direct Character Font Class
The ooodev.format.writer.direct.char.font.Font
class can set the values in Writer dialog: Character font, Writer dialog: Character font
and even more. This class is more of a goto rather than using both Write Direct Character FontOnly Class and
Write Direct Character FontEffects Class.
Setup
import sys
from ooodev.format.writer.direct.char.font import Font
from ooodev.office.write import Write
from ooodev.utils.color import CommonColor
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
def main() -> int:
with Lo.Loader(Lo.ConnectPipe(Lo.Options(verbose=True))):
doc = Write.create_doc()
GUI.set_visible(doc=doc)
Lo.delay(300)
GUI.zoom(GUI.ZoomEnum.PAGE_WIDTH)
cursor = Write.get_cursor(doc)
ft_bold = Font(b=True)
Write.append(cursor=cursor, text="Have you ", styles=[ft_bold])
Write.append(
cursor=cursor,
text="RED",
styles=[ft_bold, Font(color=CommonColor.DARK_RED)]
)
Write.append_para(
cursor=cursor, text=" this?", styles=[Font(b=True, i=True, u=True)]
)
Lo.delay(1_000)
Lo.close_doc(doc)
return 0
if __name__ == "__main__":
sys.exit(main())
Examples
Font bold, italic, underline and color
cursor = Write.get_cursor(doc)
# font bold instance
ft_bold = Font(b=True)
# append bolded text
Write.append(cursor=cursor, text="Have you ", styles=(ft_bold,))
# Combine red and bold and two Font instances
Write.append(
cursor=cursor, text="RED", styles=(ft_bold, Font(color=CommonColor.DARK_RED))
)
# Style text bold, italic,, underline
Write.append_para(cursor=cursor, text=" this?", styles=(Font(b=True, i=True, u=True),))
Alternatively Font instance can chain together properties. The last line of code above could have been written.
Write.append_para(cursor=cursor, text=" this?", styles=(Font().bold.underline.italic,))
or
Write.append_para(cursor=cursor, text=" this?", styles=(ft_bold.underline.italic,))
Font Shadowed
cursor = Write.get_cursor(doc)
ft = Font(size=17.0, shadowed=True)
Write.append(cursor=cursor, text="Shadowed", styles=(ft,))
Text with hyperlink and superscript
from ooodev.format.writer.direct.char.hyperlink import Hyperlink, TargetKind
# ... other code
cursor = Write.get_cursor(doc)
ft = Font(color=CommonColor.DARK_GREEN)
hl = Hyperlink(
name="machine_learn",
url="https://en.wikipedia.org//wiki/Machine_learning",
target=TargetKind.BLANK
)
ft_super = Font(name="Liberation Mono", superscript=True)
Write.append(
cursor=cursor, text="What do you know about machine learning?", styles=(ft,)
)
Write.append(cursor=cursor, text="[", styles=(ft_super,))
Write.append(cursor=cursor, text="1", styles=(ft_super, hl))
Write.append_para(cursor=cursor, text="]", styles=(ft_super,))