当前位置:   article > 正文

如何在 Python 中将 Excel 文件转换为图像?Aspose快速搞定_python aspose

python aspose

在各种情况下,需要将 Excel 电子表格嵌入到 Web 或桌面应用程序中。在这种情况下的解决方案之一是将 Excel 工作表转换为图像格式。在本文中,将学习如何在 Python中将Excel XLSX 或 XLS 转换为 PNG、JPEG、SVG 或其他图像。

  • 在 Python 中将 Excel 转换为图像
  • 在 Python 中将 Excel 转换为 SVG

为了将 Excel 的 XLSX 或 XLS 文件转换为图像格式,我们将使用Aspose.Cells for Python via Java它是一个电子表格操作 API,可让您创建、修改或转换 Excel 文件。你可以下载Aspose.Cells for Python


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 等的步骤。

  1. 使用Workbook类加载 Excel 文件
  2. 创建ImageOrPrintOptions类的实例并指定输出图像格式。
  3. 使用Workbook.getWorksheets().get(index)方法访问要转换的工作表。
  4. 创建一个SheetRender对象并使用Worksheet和ImageOrPrintOptions对象对其进行初始化。
  5. 使用SheetRender.toImage(pageIndex, fileName)方法将 Excel 工作表的每一页保存为图像。

以下代码示例展示了如何在 Python 中将 Excel 工作表转换为 PNG 图像。

  1. # load the Excel workbook
  2. workbook = Workbook("Book1.xlsx")
  3. # create image options
  4. imgOptions = ImageOrPrintOptions()
  5. imgOptions.setSaveFormat(SaveFormat.SVG)
  6. # load the worksheet to be rendered
  7. sheet = workbook.getWorksheets().get(0)
  8. # create sheet render object
  9. sr = SheetRender(sheet, imgOptions)
  10. # convert sheet to PNG image
  11. for j in range(0, sr.getPageCount()):
  12. sr.toImage(j, "WorksheetToImage-out%s" %(j) + ".png")

在 Python 中将 Excel 转换为 SVG

以下是在 Python 中将 Excel 文件转换为 SVG 的步骤。

  1. 使用Workbook类加载 Excel 文件。
  2. 创建ImageOrPrintOptions类的实例并指定输出图像格式。
  3. 使用Workbook.getWorksheets().getCount()方法遍历 Excel 文件中的工作表。
  4. 在每次迭代中,执行以下操作:
    1. 创建一个SheetRender对象并使用Worksheet和ImageOrPrintOptions对象对其进行初始化。
    2. 使用SheetRender.toImage(pageIndex, fileName)方法将 Excel 工作表的每一页保存为 SVG 。

以下代码示例展示了如何在 Python 中将 Excel 转换为 SVG。

  1. # load the Excel workbook
  2. workbook = Workbook("Book1.xlsx")
  3. # create image options
  4. imgOptions = ImageOrPrintOptions()
  5. imgOptions.setSaveFormat(SaveFormat.SVG)
  6. # get sheet count
  7. sheetCount = workbook.getWorksheets().getCount()
  8. # loop through the sheets
  9. for i in range(0, sheetCount):
  10. sheet = workbook.getWorksheets().get(i)
  11. # convert each sheet to SVG
  12. sr = SheetRender(sheet, imgOptions)
  13. for j in range(0, sr.getPageCount()):
  14. sr.toImage(j, sheet.getName() + "%s" % j + "_out.svg")

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),很高兴为您提供查询和咨询。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/69518
推荐阅读
相关标签
  

闽ICP备14008679号