Create and Format Paragraphs
Create Paragraphs
You can create a paragraph by using an
mlreportgen.dom.Paragraph
constructor with a character
vector. For example:
p = Paragraph('Text for a paragraph');
You can also specify these DOM objects in a Paragraph
object
constructor.
mlreportgen.dom.Text
mlreportgen.dom.ExternalLink
mlreportgen.dom.InternalLink
mlreportgen.dom.LinkTarget
mlreportgen.dom.Image
Create Headings
A heading is a type of paragraph. You can use
mlreportgen.dom.Heading1
, Heading2
, and so on, to
create headings. Alternatively, you can use an
mlreportgen.dom.Heading
object if you want to use
programmatically derived values for the heading level.
This example creates a first-level heading with the text Chapter 1:
System Overview
. If you create a table of contents, this heading
appears at the top level.
h1 = Heading1('Chapter 1: System Overview');
Format Paragraphs
You can format a paragraph using DOM format objects or format properties. You can also use template styles. For information about these formatting techniques and format inheritance, see Report Formatting Approaches.
Note
You can use the same format objects and properties for heading objects
(Heading
and Heading1
,
Heading2
, and so on) as you do for
Paragraph
objects.
Format a Paragraph Programmatically
You can use DOM API format objects to format Paragraph
objects or format properties to specify commonly used paragraph formats. This
example uses:
An
OuterMargin
format object to specify the margins for the paragraphThe
HAlign
format property to center the paragraph
import mlreportgen.dom.*; d = Document('test','html'); p = Paragraph('Indent a half inch and space after 12 points.'); p.Style = {OuterMargin('0.5in','0in','0in','12pt')}; append(d,p); p = Paragraph('Centered paragraph'); p.HAlign = 'center'; append(d,p); close(d); rptview(d.OutputPath);
Use these objects and properties to format a paragraph.
Formatting | Format Object | Format Property |
---|---|---|
Font |
|
|
Backup font (HTML only) |
| n/a |
Complex script font (for example, Arabic) |
| n/a |
East Asian font |
| n/a |
Font size |
|
|
Foreground color |
|
|
Background color |
|
|
Bold |
|
|
Italic |
|
|
Subscript or superscript |
| n/a |
Strike through |
|
|
Underline type |
|
|
Underline color |
| n/a |
Create border around paragraph |
| n/a |
Preserve white space |
|
|
Indent a paragraph |
|
|
Indent first line of paragraph |
|
|
Hanging indent |
| n/a |
Space before and after paragraph |
| n/a |
Space to right of paragraph |
| n/a |
Space between paragraph and its bounding box |
| n/a |
Space between paragraph lines |
| n/a |
Align paragraph left, center, right |
|
|
Start paragraph on next page |
| n/a |
Keep with next paragraph |
| n/a |
Keep paragraph on same page |
| n/a |
Eliminate widows and orphans |
| n/a |
Table of contents level of paragraph |
|
|
Display as specified |
| n/a |
Format Paragraphs for Microsoft Word Using Template Styles
You can format a paragraph using a style in a Word template. You can add styles to the template or modify existing ones.
To add a paragraph style:
Open the Word template used with the report.
Open the Styles pane.
Click the Manage Styles button .
Click New Style.
In the Create New Style from Formatting dialog box, set Style type to
Character
orLinked (paragraph and character)
.Format the style as needed.
For more information about working with Word styles, see Modify Styles in Microsoft Word Templates.
Format Paragraphs Using PDF or HTML Template Styles
You can format a paragraph using a style in an HTML or PDF style sheet in your template. You can add styles to the template or modify existing ones.
Define the style using a selector on a p
element. This
example defines a BodyPara
paragraph style.
p.BodyPara {
font-family: "Times New Roman", Times, serif;
font-style: normal;
font-size: 11pt;
color: black;
margin-left: 0.5in;
}
You can use any CSS properties and selectors in HTML templates. For PDF templates, you can use a subset of CSS properties and selectors. See Modify Styles in PDF Templates.
For more information about using HTML styles with DOM objects, see Modify Styles in HTML Templates.
Apply Styles to Paragraph Objects
Apply a template style to a Paragraph
object either as the
second argument in a Paragraph
object constructor or by
setting the StyleName
property on the paragraph to a template
style.
Suppose that you have defined styles named BodyPara
and
MyTitle
in a template. This example first specifies a
style name in a Paragraph
constructor. It then specifies the
style in a Paragraph
object StyleName
format property. This example assumes both styles are defined in
MyTemplate
.
import mlreportgen.dom.*; rpt = Document('MyReport','html','MyTemplate'); % Specify style name using an argument when you create the Paragraph p = Paragraph('Format this paragraph using a body style.','BodyPara'); append(rpt,p); p = Paragraph('This paragraph is formatted using a title style.'); % Specify style name using a property on the paragraph p.StyleName = 'MyTitle'; append(rpt,p); close(rpt); rptview(rpt.OutputPath);
Override Template Formats
You can use programmatic formats to override the paragraph formats defined in
a template-based paragraph style. Suppose that you define a paragraph style
named BodyPara
in your Word template and set the
KeepWithNext
property to off
. You can
override the style in your report program to keep a particular paragraph on the
same page with the next paragraph:
import mlreportgen.dom.*; rpt = Document('MyReport','docx','MyTemplate'); p = Paragraph('Keep this body paragraph with next.','BodyPara'); p.Style = {'KeepWithNext'}; append(rpt,p); p = Paragraph('Next paragraph.'); append(rpt, p); close(rpt); rptview(rpt.OutputPath);
See Also
Classes
mlreportgen.dom.Paragraph
|mlreportgen.dom.Text
|mlreportgen.dom.FontFamily
|mlreportgen.dom.FontSize
|mlreportgen.dom.Bold
|mlreportgen.dom.Italic
|mlreportgen.dom.Underline
|mlreportgen.dom.Strike
|mlreportgen.dom.KeepLinesTogether
|mlreportgen.dom.KeepWithNext
|mlreportgen.dom.PageBreakBefore
|mlreportgen.dom.LineSpacing
|mlreportgen.dom.Display