Write Modify Page Page
The ooodev.format.writer.modify.page.page.PaperFormat, ooodev.format.writer.modify.page.page.Margins,
and ooodev.format.writer.modify.page.page.LayoutSettings classes are used to modify the Page style values
seen in Fig. 1120 of a Page style.
Fig. 1120 Writer dialog Page default
Default Page Style Dialog
Setup
General function used to run these examples.
from ooodev.format.writer.modify.page.page import LayoutSettings, Margins, PaperFormat
from ooodev.format.writer.modify.page.page import PageStyleLayout, NumberingTypeEnum
from ooodev.format.writer.modify.page.page import PaperFormatKind, WriterStylePageKind
from ooodev.utils.data_type.size_mm import SizeMM
from ooodev.units import UnitInch
from ooodev.office.write import Write
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
def main() -> int:
with Lo.Loader(Lo.ConnectPipe()):
doc = Write.create_doc()
GUI.set_visible(doc=doc)
Lo.delay(300)
GUI.zoom(GUI.ZoomEnum.ENTIRE_PAGE)
layout_style = LayoutSettings(
layout=PageStyleLayout.MIRRORED,
numbers=NumberingTypeEnum.CHARS_UPPER_LETTER,
ref_style=WriterStylePageKind.TEXT_BODY,
style_name=WriterStylePageKind.STANDARD,
)
layout_style.apply(doc)
style_obj = LayoutSettings.from_style(doc=doc, style_name=WriterStylePageKind.STANDARD)
assert style_obj.prop_style_name == str(WriterStylePageKind.STANDARD)
Lo.delay(1_000)
Lo.close_doc(doc)
return 0
if __name__ == "__main__":
SystemExit(main())
Page Paper Format
The PaperFormat class is used to modify the paper format of a page style.
Setting Paper Format from a preset
The PaperFormatKind class is used to look up the preset of paper format for convenience.
# ... other code
paper_fmt_style = PaperFormat.from_preset(
preset=PaperFormatKind.A3, landscape=True, style_name=WriterStylePageKind.STANDARD
)
paper_fmt_style.apply(doc)
Style results.
Fig. 1121 Writer dialog Page style Paper Format changed
Setting Paper to Custom Format
It is possible to set the page to a custom format by using the PaperFormat class.
The constructor takes a SizeMM object which can also take Class UnitT object for width and height.
The UnitInch supports UnitT and is used to set the page size in inches.
If the width is greater than the height then the page is set to landscape; Otherwise, the page is set to portrait.
# ... other code
paper_fmt_style = PaperFormat(
size=SizeMM(width=UnitInch(11), height=UnitInch(8.5)),
style_name=WriterStylePageKind.STANDARD,
)
paper_fmt_style.apply(doc)
Style results.
Fig. 1122 Writer dialog Page style Paper Format changed
Getting paper format from a style
We can get the border sides from the document.
# ... other code
style_obj = PaperFormat.from_style(doc=doc, style_name=WriterStylePageKind.STANDARD)
assert style_obj.prop_style_name == str(WriterStylePageKind.STANDARD)
Page Margins
Setting Page Margins using MM units
The Margins class is used to modify the margins of a page style.
In this case the margins are set to mm values which is the default unit of the class.
The result are seen in Fig. 1123.
# ... other code
margin_style = Margins(
left=30,
right=30,
top=35,
bottom=15,
gutter=10,
style_name=WriterStylePageKind.STANDARD,
)
margin_style.apply(doc)
Style results.
Fig. 1123 Writer dialog Page margins style changed
Setting Page Margins using other units
The margins can be set using a different unit. The parameters used to set the margin size also support Class UnitT objects.
The UnitInch supports UnitT and is used to set the page margin in inches.
The result are seen in Fig. 1124.
# ... other code
margin_style = Margins(
left=UnitInch(1.0),
right=UnitInch(1.0),
top=UnitInch(1.5),
bottom=UnitInch(0.75),
gutter=UnitInch(0.5),
style_name=WriterStylePageKind.STANDARD,
)
margin_style.apply(doc)
Style results.
Fig. 1124 Writer dialog Page margins style set using inches
Getting margins from a style
# ... other code
style_obj = Margins.from_style(doc=doc, style_name=WriterStylePageKind.STANDARD)
assert style_obj.prop_style_name == str(WriterStylePageKind.STANDARD)
Page Layout
Setting Page Layout
The LayoutSettings class is used to modify the layout of a page style.
The result are seen in Fig. 1125.
# ... other code
layout_style = LayoutSettings(
layout=PageStyleLayout.MIRRORED,
numbers=NumberingTypeEnum.CHARS_UPPER_LETTER,
ref_style=WriterStylePageKind.SUBTITLE,
right_gutter=True,
gutter_pos_left=False,
style_name=WriterStylePageKind.STANDARD,
)
layout_style.apply(doc)
Style results.
Fig. 1125 Writer dialog Page Layout style changed
Getting layout from a style
# ... other code
style_obj = LayoutSettings.from_style(doc=doc, style_name=WriterStylePageKind.STANDARD)
assert style_obj.prop_style_name == str(WriterStylePageKind.STANDARD)