删除Excel文件中工作表的现有打印设置

可能的使用场景

有时开发者希望阻止 Excel 在保存的 XLSX 文件中包含 .bin 格式的打印机设置文件。打印机设置文件位于 “[file “root”]\xl\printerSettings” 位置。本文介绍如何使用 Aspose.Cells for Python via .NET API 移除现有的打印机设置。

删除Excel文件中工作表的现有打印设置

Aspose.Cells for Python via .NET 允许你删除为Excel文件中不同工作表指定的现有打印机设置。以下示例代码演示如何删除工作簿中所有工作表的现有打印机设置。请参阅其sample Excel文件输出Excel文件、控制台输出以及截图作为参考。

屏幕截图

todo:image_alt_text

示例代码

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

控制台输出

 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.