Working with Background in ODS Files

Background in ODS Files

Background can be added to sheets in ODS files. The background can either be a colored background or graphic background. The background is not visible when the file is open but if the file is printed as PDF, the background is visible in the generated PDF. The background is also visible in the print preview dialogue.

Aspose.Cells for Python via .NET provides the ability to read the background information and add the background in ODS files.

Read Background Information from ODS file

Aspose.Cells for Python via .NET provides the OdsPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of OdsPageBackground class by loading the source ODS file and reading the background information. Please see the Console Output generated by the code for reference.

Sample Code

from aspose.cells import Workbook
from PIL import Image
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = ""
# Output directory
outputDir = ""
# Load source Excel file
workbook = Workbook(sourceDir + "GraphicBackground.ods")
# Access first worksheet
worksheet = workbook.worksheets[0]
background = worksheet.page_setup.ods_page_background
print("Background Type: " + str(background.type))
print("Backgorund Position: " + str(background.graphic_position_type))
image = Image.frombytes(mode='RGB', size=(50,50), data=background.graphic_data)
image.save(outputDir + "background.jpg")

Console Output

Background Type: Graphic

Backgorund Position: CenterCenter

Add Colored Background to ODS file

Aspose.Cells for Python via .NET provides the OdsPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of OdsPageBackground.color property to add a color background to the ODS file. Please see the output ODS file generated by the code for reference.

Sample Code

from aspose.cells import SaveFormat, Workbook
from aspose.cells.ods import OdsPageBackgroundType
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
workbook = Workbook()
# Access first worksheet
worksheet = workbook.worksheets[0]
worksheet.cells.get(0, 0).value = 1
worksheet.cells.get(1, 0).value = 2
worksheet.cells.get(2, 0).value = 3
worksheet.cells.get(3, 0).value = 4
worksheet.cells.get(4, 0).value = 5
worksheet.cells.get(5, 0).value = 6
worksheet.cells.get(0, 1).value = 7
worksheet.cells.get(1, 1).value = 8
worksheet.cells.get(2, 1).value = 9
worksheet.cells.get(3, 1).value = 10
worksheet.cells.get(4, 1).value = 11
worksheet.cells.get(5, 1).value = 12
background = worksheet.page_setup.ods_page_background
background.color = Color.red
background.type = OdsPageBackgroundType.COLOR
workbook.save(outputDir + "ColoredBackground.ods", SaveFormat.ODS)

Add Graphic Background to ODS file

Aspose.Cells for Python via .NET provides the OdsPageBackground class to manage background in ODS Files. The following code sample demonstrates the use of OdsPageBackground.graphic_data property to add graphic background to the ODS file. Please see the output ODS file generated by the code for reference.

Sample Code

from aspose.cells import SaveFormat, Workbook
from aspose.cells.ods import OdsPageBackgroundGraphicType, OdsPageBackgroundType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Instantiating a Workbook object
workbook = Workbook()
# Access first worksheet
worksheet = workbook.worksheets[0]
worksheet.cells.get(0, 0).value = 1
worksheet.cells.get(1, 0).value = 2
worksheet.cells.get(2, 0).value = 3
worksheet.cells.get(3, 0).value = 4
worksheet.cells.get(4, 0).value = 5
worksheet.cells.get(5, 0).value = 6
worksheet.cells.get(0, 1).value = 7
worksheet.cells.get(1, 1).value = 8
worksheet.cells.get(2, 1).value = 9
worksheet.cells.get(3, 1).value = 10
worksheet.cells.get(4, 1).value = 11
worksheet.cells.get(5, 1).value = 12
background = worksheet.page_setup.ods_page_background
background.type = OdsPageBackgroundType.GRAPHIC
background.graphic_data = open(sourceDir + "background.jpg", "rb").read()
background.graphic_type = OdsPageBackgroundGraphicType.AREA
workbook.save(outputDir + "GraphicBackground.ods", SaveFormat.ODS)