Remove Existing PrinterSettings of Worksheets in Excel file

Possible Usage Scenarios

Sometimes developers want to prevent Excel from including .bin files of printer settings in the saved XLSX files. Printer settings files are located under “[file “root”]\xl\printerSettings”. This document explains how to remove existing printer settings using Aspose.Cells for Python via .NET APIs.

Remove Existing PrinterSettings of Worksheets in Excel file

Aspose.Cells for Python via .NET allows you to remove existing printer settings specified for different sheets in the Excel file. The following sample code illustrates how to remove existing printer settings for all the worksheets in the workbook. Please see its sample Excel file, output Excel file, console output as well as the screenshot for a reference.

Screenshot

todo:image_alt_text

Sample Code

from aspose.cells import Workbook
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Load source Excel file
wb = Workbook(sourceDir + "sampleRemoveExistingPrinterSettingsOfWorksheets.xlsx")
# Get the sheet counts of the workbook
sheetCount = len(wb.worksheets)
# Iterate all sheets
for i in range(sheetCount):
# Access the i-th worksheet
ws = wb.worksheets[i]
# Access worksheet page setup
ps = ws.page_setup
# Check if printer settings for this worksheet exist
if ps.printer_settings != None:
# Print the following message
print("PrinterSettings of this worksheet exist.")
# Print sheet name and its paper size
print("Sheet Name: " + ws.name)
print("Paper Size: " + str(ps.paper_size))
# Remove the printer settings by setting them null
ps.printer_settings = None
print("Printer settings of this worksheet are now removed by setting it null.")
print("")
# Save the workbook
wb.save(outputDir + "outputRemoveExistingPrinterSettingsOfWorksheets.xlsx")

Console Output

 PrinterSettings of this worksheet exist.

Sheet Name: Sheet1

Paper Size: PaperLegal

Printer settings of this worksheet are now removed by setting it null.

PrinterSettings of this worksheet exist.

Sheet Name: Sheet2

Paper Size: PaperEnvelopeB5

Printer settings of this worksheet are now removed by setting it null.

PrinterSettings of this worksheet exist.

Sheet Name: Sheet3

Paper Size: PaperA6

Printer settings of this worksheet are now removed by setting it null.

PrinterSettings of this worksheet exist.

Sheet Name: Sheet4

Paper Size: PaperA3

Printer settings of this worksheet are now removed by setting it null.