页面设置和打印选项

处理页面和打印设置

在本例中,我们创建了一个 Excel 工作簿,使用 Aspose.Cells for Python via .NET 设置页面布局和打印选项。

使用 Aspose.Cells 设置页面设置选项

首先在Microsoft Excel中创建一个简单的工作表。然后将页面设置选项应用于它。执行代码将更改页面设置选项,如下面的屏幕截图所示。

输出文件。
todo:image_alt_text
  1. 在Microsoft Excel中创建一个带有一些数据的工作表:
    1. 在Microsoft Excel中打开一个新的工作簿。
    2. 添加一些数据。
  2. 设置页面设置选项: 将页面设置选项应用于文件。以下是应用新选项之前的默认选项的屏幕截图。
默认页面设置选项。
todo:image_alt_text
from aspose.cells import PageOrientationType, PaperSizeType, Workbook
# 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(".")
# Open the template workbook
workbook = Workbook(dataDir + "CustomerReport.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the orientation to Portrait
worksheet.page_setup.orientation = PageOrientationType.PORTRAIT
# Setting the scaling factor to 100
# worksheet.PageSetup.Zoom = 100;
# OR Alternately you can use Fit to Page Options as under
# Setting the number of pages to which the length of the worksheet will be spanned
worksheet.page_setup.fit_to_pages_tall = 1
# Setting the number of pages to which the width of the worksheet will be spanned
worksheet.page_setup.fit_to_pages_wide = 1
# Setting the paper size to A4
worksheet.page_setup.paper_size = PaperSizeType.PAPER_A4
# Setting the print quality of the worksheet to 1200 dpi
worksheet.page_setup.print_quality = 1200
# Setting the first page number of the worksheet pages
worksheet.page_setup.first_page_number = 2
# Save the workbook
workbook.save(dataDir + "PageSetup_out.xlsx")

设置打印选项

页面设置还提供了几个打印选项(也称为工作表选项),允许用户控制工作表页面的打印方式。它们允许用户:

  • 选择工作表的特定打印区域。
  • 打印标题。
  • 打印网格线。
  • 打印行/列标题。
  • 获得草稿质量。
  • 打印注释。
  • 打印单元格错误。
  • 定义页面排序。

下面的示例将打印选项应用于上面示例中创建的文件(PageSetup.xls)。下面的屏幕截图显示了应用新选项之前的默认打印选项。

输入文档
todo:image_alt_text
执行代码会更改打印选项。
输出文件
todo:image_alt_text
from aspose.cells import PrintCommentsType, PrintErrorsType, PrintOrderType, Workbook
# 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(".")
# Open the template workbook
workbook = Workbook(dataDir + "PageSetup.xlsx")
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
pageSetup = worksheet.page_setup
# Specifying the cells range (from A1 cell to E30 cell) of the print area
pageSetup.print_area = "A1:E30"
# Defining column numbers A & E as title columns
pageSetup.print_title_columns = "$A:$E"
# Defining row numbers 1 as title rows
pageSetup.print_title_rows = "$1:$2"
# Allowing to print gridlines
pageSetup.print_gridlines = True
# Allowing to print row/column headings
pageSetup.print_headings = True
# Allowing to print worksheet in black & white mode
pageSetup.black_and_white = True
# Allowing to print comments as displayed on worksheet
pageSetup.print_comments = PrintCommentsType.PRINT_IN_PLACE
# Allowing to print worksheet with draft quality
pageSetup.print_draft = True
# Allowing to print cell errors as N/A
pageSetup.print_errors = PrintErrorsType.PRINT_ERRORS_NA
# Setting the printing order of the pages to over then down
pageSetup.order = PrintOrderType.OVER_THEN_DOWN
# Save the workbook
workbook.save(dataDir + "PageSetup_Print_out.xlsx")