ヘッダーとフッターの設定

ヘッダーとフッタの設定

Aspose.Cells for Python via .NET を使用すると、実行時にワークシートにヘッダーとフッターを追加できますが、印刷のために事前にデザインされたファイルで手動で設定することをお勧めします。Microsoft ExcelをGUIツールとして使用してヘッダーとフッターを設定し、労力と開発時間を節約できます。Aspose.Cells for Python via .NET はファイルをインポートして設定を保存できます。

実行時にヘッダーとフッターを追加するには、Aspose.Cells for Python via .NET がヘッダーとフッターの書式設定に役立つ特別なAPI呼び出しとスクリプトコマンドを提供します。

スクリプトコマンド

スクリプトコマンドは、ヘッダーやフッターのフォーマットを設定する特別なコマンドです。

スクリプトコマンド 説明
&P 現在のページ番号
&G 画像
&N ページの総数
&D 現在の日付
&T 現在の時刻
&A ワークシート名
&F パスを除いたファイル名
&"<FontName>" フォント名を表します。例: &“Arial”
&"<FontName>, <FontStyle>" スタイル付きのフォント名を表します。例: &“Arial,Bold”
&<FontSize> フォントサイズを表します。例:「&14abc」。ただし、このコマンドの後にヘッダーに印刷されるプレーンな数字が続く場合、その数字はフォントサイズとスペースで区切る必要があります。例:「&14 123」。

ヘッダーとフッターを設定する方法

PageSetup クラスは、ワークシートにヘッダーやフッターを追加するために使用される set_headerset_footer という二つのメソッドを提供します。これらのメソッドは2つのパラメーターのみを取ります。

  • Section – ヘッダーやフッターを配置するセクション。左、中央、右の3つのセクションがあり、それぞれ0、1、2で表されます。
  • Script – ヘッダーやフッターのために使用するスクリプト。このスクリプトにはヘッダーやフッターをフォーマットするためのスクリプトコマンドが含まれます。
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Instantiating a Workbook object
excel = Workbook()
# Obtaining the reference of the PageSetup of the worksheet
pageSetup = excel.worksheets[0].page_setup
# Setting worksheet name at the left section of the header
pageSetup.set_header(0, "&A")
# Setting current date and current time at the centeral section of the header
# and changing the font of the header
pageSetup.set_header(1, "&\"Times New Roman,Bold\"&D-&T")
# Setting current file name at the right section of the header and changing the
# font of the header
pageSetup.set_header(2, "&\"Times New Roman,Bold\"&12&F")
# Setting a string at the left section of the footer and changing the font
# of a part of this string ("123")
pageSetup.set_footer(0, "Hello World! &\"Courier New\"&14 123")
# Setting the current page number at the central section of the footer
pageSetup.set_footer(1, "&P")
# Setting page count at the right section of footer
pageSetup.set_footer(2, "&N")
# Save the Workbook.
excel.save("SetHeadersAndFooters_out.xls")

ヘッダーまたはフッターに画像を挿入する方法

PageSetup クラスには、ヘッダーやフッターに画像を追加するために使用される set_header_pictureset_footer_picture という追加のメソッドがあります。これらのメソッドは以下のパラメーターを取ります。

  • Section – 画像が配置されるヘッダーやフッターセクション。左、中央、右の3つのセクションがあり、それぞれ0、1、2で表されます。
  • バイト配列 – グラフィカルデータ(バイナリデータはバイト配列のバッファに書き込む必要があります)。

以下のコードを実行し、ファイルを開いた後、ワークシートのヘッダーを確認してください。

  1. ファイル メニューから ページ設定 を選択します。ダイアログが表示されます。
  2. ヘッダー/フッター タブを選択します。
from aspose.cells import Workbook
import bytearray
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a Workbook object
workbook = Workbook()
# Creating a string variable to store the url of the logo/picture
logo_url = dataDir + "aspose-logo.jpg"
# Creating the instance of the FileStream object to open the logo/picture in the stream
inFile = open(logo_url, "rb")
# Instantiating the byte array of FileStream object's size
binaryData = bytearray(utils.filesize(inFile))
# Reads a block of bytes from the stream and writes data in a given buffer of byte array.
bytesRead = inFile.readinto(binaryData)
# Creating a PageSetup object to get the page settings of the first worksheet of the workbook
pageSetup = workbook.worksheets[0].page_setup
# Setting the logo/picture in the central section of the page header
pageSetup.set_header_picture(1, binaryData)
# Setting the script for the logo/picture
pageSetup.set_header(1, "&G")
# Setting the Sheet's name in the right section of the page header with the script
pageSetup.set_header(2, "&A")
# Saving the workbook
workbook.save(dataDir + "InsertImageInHeaderFooter_out.xls")
# Closing the FileStream object
inFile.close()