Main Content

Number Pages in a Word Template

This example shows how to number pages of a report using a Word template. It defines a right-aligned page number for odd pages and a left-aligned page number for even pages by using two different footers. The following image shows two pages of a document created using the example template:

Two-page document layout showing mostly blank white pages with light crop marks in the corners. The left page includes small text near the top left reading “hello world” and “Page 1” centered at the bottom. The right page includes small text near the top left reading “hello again” and “Page 2” centered at the bottom.

Create a Template

Create a Word template, myWordTemplate.dotx, in the current working directory. Open the file in Word. A reference template that already includes the footers for the example is available in exampleTemplate.dotx.

mlreportgen.dom.Document.createTemplate("myWordTemplate","docx");

Add a Footer and Page Number

In Word, edit the template footer by double-clicking the footer area. This opens a Header and Footer Tools Design tab and moves the cursor into the footer area. Type what should appear before the page number in the footer. In the following example, "Page " precedes the page number:

Product screenshot of a Word document in Header & Footer editing mode, showing the Design tab ribbon with options for navigation, header and footer settings, and positioning. The footer area is active with the cursor placed before the typed word “Page” near the bottom of the page.

In the Header and Footer Tools Design tab open the Page Number menu and select Plain Number from the Current Position options. This will insert a page number at the current position of the cursor.

Product screenshot of the Insert tab in Word with the Page Number menu expanded. Options shown include Top of Page, Bottom of Page, Page Margins, Current Position, Format Page Numbers, and Remove Page Numbers. A preview pane on the right displays the “Plain Number” option with the number “1” positioned at the current cursor location in the document.

Open the Page Number menu again and select Format Page Numbers... In the Page Number Format menu, set the starting number to 1 and select OK.

Dialog box titled “Page Number Format” displaying options for customizing page numbering in Word. Controls include a dropdown for number format, an optional chapter-number setting with style and separator choices, and radio buttons to either continue numbering from the previous section or start at a specified number. Buttons for OK and Cancel appear at the bottom.

Format the footer text as you would format any text within Word. In this example, the font size is set to 14 and the text style is bold. Set the horizontal alignment of the text to be right-aligned.

Create a Different Even Page Footer

To specify a different page footer for even and odd pages, select the Different Odd & Even Pages option in the Header and Footer Tools Design tab.

Product screenshot of the Word Header & Footer Design tab showing three option checkboxes: “Different First Page” unchecked, “Different Odd & Even Pages” checked, and “Show Document Text” checked.

To view the even page footer, add another page to the document. Double-click in the body section of the document or click Close Header and Footer to edit the main body of the document. Insert a page break by clicking the Page Break button in the Insert tab or by pressing CTRL + Enter.

Double-click the footer area in the new page to edit the even page footer. Repeat the earlier process to add text and a page number to the even page footer. Leave the horizontal alignment of the text as the default left-aligned. There is no need to modify the starting number in the Page Number Format menu because the page number is automatically increased from the previous page.

Product screenshot showing the footer area of a Word document labeled “Even Page Footer,” with the cursor positioned in the empty footer space below a dotted boundary line. Provide your feedback on BizChat

Return to editing the main body of the document. Delete the second page of the document and save the template.

Use the Template

Use the template by specifying the template name when creating the document. The following code uses the reference template exampleTemplate.dotx to create a document. To use the template modified by the example, replace exampleTemplate with myWordTemplate.

import mlreportgen.dom.*

d = Document("myDocument", "docx", "exampleTemplate");
open(d);

append(d, "Hello world");
append(d, PageBreak());
append(d, "Hello again");
append(d, PageBreak());
append(d, "Hello again");
append(d, PageBreak());
append(d, "Hello again");
append(d, PageBreak());
append(d, "Hello again");

close(d);
rptview(d);

See Also

Topics