- 在 Python 中将 Excel 转换为图像
- 在 Python 中将 Excel 转换为 SVG
为了将 Excel 的 XLSX 或 XLS 文件转换为图像格式,我们将使用Aspose.Cells for Python via Java它是一个电子表格操作 API,可让您创建、修改或转换 Excel 文件。你可以点击下方按钮获取使用。
好消息来啦!整合所有格式API处理控件Aspose永久授权正在 火热销售中!联系客服立马1分钟了解全部咨询!
Aspose.Cells for Python via Java 支持将 Excel 文件转换为以下图像格式:
- EMF
- WMF
- JPEG
- PNG
- BMP
- GIF
- TIFF
- SVG
- GLTF
- PICT
- SVM
- Office Compatible EMF
在 Python 中将 Excel 转换为图像
以下是将 Excel 文件转换为图像格式,即 PNG、JPEG 等的步骤。
- 使用Workbook类加载 Excel 文件
- 创建ImageOrPrintOptions类的实例并指定输出图像格式。
- 使用Workbook.getWorksheets().get(index)方法访问要转换的工作表。
- 创建一个SheetRender对象并使用Worksheet和ImageOrPrintOptions对象对其进行初始化。
- 使用SheetRender.toImage(pageIndex, fileName)方法将 Excel 工作表的每一页保存为图像。
以下代码示例展示了如何在 Python 中将 Excel 工作表转换为 PNG 图像。
# load the Excel workbookworkbook = Workbook("Book1.xlsx")# create image optionsimgOptions = ImageOrPrintOptions()imgOptions.setSaveFormat(SaveFormat.SVG)# load the worksheet to be renderedsheet = workbook.getWorksheets().get(0)# create sheet render objectsr = SheetRender(sheet, imgOptions)# convert sheet to PNG imagefor j in range(0, sr.getPageCount()):sr.toImage(j, "WorksheetToImage-out%s" %(j) + ".png")
在 Python 中将 Excel 转换为 SVG
以下是在 Python 中将 Excel 文件转换为 SVG 的步骤。
- 使用Workbook类加载 Excel 文件。
- 创建ImageOrPrintOptions类的实例并指定输出图像格式。
- 使用Workbook.getWorksheets().getCount()方法遍历 Excel 文件中的工作表。
-
在每次迭代中,执行以下操作:
- 创建一个SheetRender对象并使用Worksheet和ImageOrPrintOptions对象对其进行初始化。
- 使用SheetRender.toImage(pageIndex, fileName)方法将 Excel 工作表的每一页保存为 SVG 。
以下代码示例展示了如何在 Python 中将 Excel 转换为 SVG。
# load the Excel workbookworkbook = Workbook("Book1.xlsx")# create image optionsimgOptions = ImageOrPrintOptions()imgOptions.setSaveFormat(SaveFormat.SVG)# get sheet countsheetCount = workbook.getWorksheets().getCount()# loop through the sheetsfor i in range(0, sheetCount):sheet = workbook.getWorksheets().get(i)# convert each sheet to SVGsr = SheetRender(sheet, imgOptions)for j in range(0, sr.getPageCount()): sr.toImage(j, sheet.getName() + "%s" % j + "_out.svg")
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!