这个简单的主题指导如何使用 Python 插入 Excel 背景图像。它包含有关开发应用程序时所需的配置和所需的语言构造的信息。使用 Python 插入 Excel 背景图像后,DOM 中的输出文件可以保存为 XLSX、XLS 或任何其他支持的格式。
使用 Python 插入 Excel 背景图像的步骤
- 配置 IDE 以与 Aspose.Cells for Python via Java 一起插入工作表背景图像
- 使用 Workbook 类的实例创建新的 Excel 电子表格
- 访问第一个默认的 Worksheet,其中图像将被添加为背景
- 从要设置的图像文件中读取所有字节
- 通过提供字节数组设置工作表的BackgroundImage方法
- 保存具有工作表背景图像的工作簿
上述过程描述了使用Python插入电子表格背景图像的过程。这里介绍了用于添加图像背景的所有必需的类和方法。该过程相当简单,因为图像文件被读入字节数组并使用 setBackgroundImage 方法将其设置为工作表背景图像。
使用 Python 为 Excel 工作表插入背景图片的示例代码
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import License, Workbook, Worksheet, SaveFormat | |
#Instantiate the product license | |
license = License() | |
license.setLicense("Aspose.Total.lic") | |
#Create a default Excel workbook | |
workbook = Workbook() | |
#Access the first worksheet | |
worksheet = workbook.getWorksheets().get(0) | |
#Access the source image as a byte array from the disk | |
with open("sample.png", "rb") as bin_file: | |
#Read the entire image file at once from the disk | |
imageData = bin_file.read() | |
#Set the worksheet background image method | |
worksheet.setBackgroundImage(imageData); | |
#Save the output Excel file with the background image | |
workbook.save("ExcelSheetBackgroundWithImage.xlsx") | |
print("Background added successfully") | |
jpype.shutdownJVM() |
此示例展示了使用 Python 添加 Excel 图像背景的过程。第一步,创建工作簿或从磁盘加载工作簿,然后使用要设置背景图像的索引访问所需的工作表。使用所需图像的字节数据,该图像可以存储在磁盘上,也可以根据要求保存在数据库或任何其他源(如 Web API)中。
这个例子教会我们将背景图像插入工作簿中的工作表。如果您有兴趣了解将 XML 转换为 Excel 文件的过程,请参阅有关 如何使用 Python 将 XML 转换为 Excel 文件 的文章。