Impostazione Intestazioni e piè di pagina

Impostazione di intestazioni e piè di pagina

Aspose.Cells per Python via .NET consente di aggiungere intestazioni e piè di pagina ai fogli di lavoro in tempo reale, ma si consiglia di impostare manualmente intestazioni e piè di pagina in un file pre-progettato per la stampa. Puoi usare Microsoft Excel come strumento GUI per impostare intestazioni e piè di pagina per risparmiare tempo di sforzo e sviluppo. Aspose.Cells per Python via .NET può importare il file e salvare le impostazioni.

Per aggiungere intestazioni e piè di pagina in tempo reale, Aspose.Cells per Python via .NET fornisce chiamate API speciali e comandi script per formattare intestazioni e piè di pagina.

Comandi di script

I comandi di script sono comandi speciali che consentono di impostare la formattazione di intestazioni e piè di pagina.

Comandi di script Descrizione
&P Numero di pagina corrente
&G Un’immagine
&N Il numero totale di pagine
&D La data corrente
&T L’orario corrente
&A Il nome del foglio di lavoro
&F Il nome del file senza percorso
&"<FontName>" Rappresenta un nome di carattere. Ad esempio: &“Arial”
&"<FontName>, <FontStyle>" Rappresenta il nome del carattere con lo stile. Ad esempio: &“Arial,Grassetto”
&<FontSize> Rappresenta la dimensione del carattere. Ad esempio: “&14abc”. Ma, se questo comando è seguito da un numero normale da stampare nell’intestazione, questo dovrebbe essere separato da un carattere spazio dalla dimensione del carattere. Ad esempio: “&14 123”.

Come impostare intestazioni e piè di pagina

La classe PageSetup fornisce due metodi, set_header e set_footer, utilizzati per aggiungere un’intestazione e un piè di pagina a un foglio di lavoro. Questi metodi richiedono solo due parametri:

  • Sezione – la sezione in cui dovrebbe essere posizionata l’intestazione o il piè di pagina. Ci sono tre sezioni: sinistra, centro e destra, rappresentate rispettivamente da 0, 1 e 2.
  • Script – lo script da utilizzare per l’intestazione o il piè di pagina. Questo script contiene comandi di script per formattare l’intestazione o il piè di pagina.
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")

Come inserire un’immagine in un’intestazione o un piè di pagina

La classe PageSetup ha due metodi aggiuntivi, set_header_picture e set_footer_picture, utilizzati per aggiungere immagini all’intestazione e al piè di pagina. Questi metodi accettano i parametri:

  • Sezione – la sezione dell’intestazione o del piè di pagina in cui verrà posizionata l’immagine. Ci sono tre sezioni, sinistra, centro e destra, rappresentate dai valori 0, 1 e 2 rispettivamente.
  • Array di byte – i dati grafici (i dati binari devono essere scritti nel buffer di un array di byte).

Dopo aver eseguito il codice sottostante e aperto il file, controlla l’intestazione del foglio di lavoro:

  1. Nel menu File, seleziona Imposta pagina. Verrà visualizzata una finestra di dialogo.
  2. Seleziona la scheda Intestazione/Piè di pagina.
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()