メインコンテンツ

Word テンプレートのページ番号付け

この例では、Word テンプレートを使用してレポートのページに番号を付ける方法を示します。2 つの異なるフッターを使用して、奇数ページ用の右揃えのページ番号と偶数ページ用の左揃えのページ番号を定義します。次のイメージは、サンプル テンプレートを使用して作成されたドキュメントの 2 つのページを示しています。

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.

テンプレートの作成

現在の作業ディレクトリに Word テンプレート myWordTemplate.dotx を作成します。ファイルを Word で開きます。例のフッターを既に含んでいる参照テンプレートが、exampleTemplate.dotx にあります。

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

フッターとページ番号の追加

Word で、フッター領域をダブルクリックして、テンプレートのフッターを編集します。これにより、[ヘッダー/フッター ツール] の [デザイン] タブが開き、カーソルがフッター領域に移動します。フッターのページ番号の前に表示する内容を入力します。次の例では、ページ番号の直前に "Page" があります。

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.

[ヘッダー/フッター ツール] の [デザイン] タブで [ページ番号] メニューを開き、[現在の位置] オプションから [番号のみ] を選択します。これにより、カーソルの現在の位置にページ番号が挿入されます。

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.

[ページ番号] メニューをもう一度開き、[ページ番号の書式設定...] を選択します。[ページ番号の書式] メニューで、開始番号を 1 に設定し、[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.

Word 内の任意のテキストを書式設定するのと同じように、フッター テキストを書式設定します。この例では、フォント サイズは 14 に、テキスト スタイルは太字に設定されています。テキストの水平方向の配置を右揃えに設定します。

異なる偶数ページ フッターの作成

偶数ページと奇数ページに異なるページ フッターを指定するには、[ヘッダー/フッター ツール] の [デザイン] タブで [奇数/偶数ページ別指定] オプションを選択します。

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.

偶数ページのフッターを表示するために、ドキュメントに別のページを追加します。ドキュメントの本文セクションをダブルクリックするか、[ヘッダーとフッターを閉じる] をクリックして、ドキュメントの本文を編集します。[挿入] タブの [ページ区切り] ボタンをクリックするか、CTRL+Enter を押して、改ページを挿入します。

新しいページのフッター領域をダブルクリックして、偶数ページのフッターを編集します。前のプロセスを繰り返して、偶数ページのフッターにテキストとページ番号を追加します。テキストの水平方向の配置は、既定の左揃えのままにします。ページ番号は前のページから自動的に増加するため、[ページ番号の書式] メニューで開始番号を変更する必要はありません。

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

ドキュメントの本文の編集に戻ります。ドキュメントの 2 ページ目を削除し、テンプレートを保存します。

テンプレートの使用

ドキュメント作成時にテンプレート名を指定して、テンプレートを使用します。次のコードは、参照テンプレート exampleTemplate.dotx を使用してドキュメントを作成します。この例で変更したテンプレートを使用するには、exampleTemplatemyWordTemplate に置き換えます。

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);

参考

トピック