想在Python应用程序中创建Excel吗?来看看这份Aspose.Cells for Python via Java指南

在过去的几年中,Python已成为主要的编程语言之一。本文旨在将Python和电子表格自动化结合在一起,向您展示如何使用Python创建Excel XSL / XLSX文件。

Aspose.Cells for Python via Java是一个功能强大但易于使用的电子表格处理API,可让您使用Python在应用程序中实现电子表格自动化。您可以用几行代码创建新的Excel文件,以及更新和转换现有的电子表格文档。

在过去的几年中,Python已成为主要的编程语言之一。Python的实用性和受欢迎程度极大地发展了Python爱好者社区。另一方面,电子表格自动化使从Web或桌面应用程序中保存,组织和播放大量数据变得更加容易。

本文旨在将Python和电子表格自动化结合在一起,向您展示如何使用Python创建Excel XSL / XLSX文件。此外,您还将学习如何使用Python以编程方式在Excel工作表中插入数据,图像,图表和表格。

  • 使用Python创建Excel XLS / XLSX文件
  • 使用Python将数据插入到现有的Excel文件中
  • 使用Python创建具有图像的Excel文件
  • 使用Python在Excel工作表中生成图表
  • 使用Python在Excel工作表中创建数据透视表

好消息来啦!整合所有格式API处理控件Aspose永久授权正在网火热销售中,新购乐享85折起!联系客服立马1分钟了解全部咨询!


使用Python创建Excel XLS / XLSX文件

首先,通过Java使用Aspose.Cells for Python创建一个简单的Excel XLSX文件。以下是执行此操作的步骤:

  1. 创建一个Workbook类的新对象。
  2. 在工作簿中使用Workbook.getWorksheets().get(index)方法访问所需的工作表。
  3. 使用Worksheet.getCells().get(“A1”).putValue()方法将值放入所需的单元格中。
  4. 使用Workbook.save()方法将工作簿保存为.xlsx文件。

下面的代码示例演示如何使用Python创建Excel XLSX文件。

# create a new XLSX workbookwb = Workbook(FileFormatType.XLSX)# insert value in the cellswb.getWorksheets().get(0).getCells().get("A1").putValue("Hello World!")# save workbook as .xlsx filewb.save("workbook.xlsx")

想在Python应用程序中创建Excel吗看看这份Aspose.Cells for Python via Java指南

使用Python将数据插入到现有的Excel文件中

在上一个示例中,您从头创建了一个新的Excel XLSX文件。但是,在某些情况下,您需要更新现有Excel文件的内容。在这种情况下,可以通过提供工作簿构造函数的路径来加载Excel文件。用于访问工作表和单元格的其余方法将保持不变。

以下代码示例显示了如何使用Python更新Excel文件。

# create a new XLSX workbookwb = Workbook("workbook.xlsx")# insert value in the cellswb.getWorksheets().get(0).getCells().get("A1").putValue("Location")wb.getWorksheets().get(0).getCells().get("B1").putValue("Person")wb.getWorksheets().get(0).getCells().get("A2").putValue("Home")wb.getWorksheets().get(0).getCells().get("B2").putValue("abc")wb.getWorksheets().get(0).getCells().get("A3").putValue("Office")wb.getWorksheets().get(0).getCells().get("B3").putValue("xyz")# save workbook as .xlsx filewb.save("workbook-updated.xlsx")

想在Python应用程序中创建Excel吗看看这份Aspose.Cells for Python via Java指南

使用Python创建具有图像的Excel文件

在前面的两个示例中,您已经了解了如何在Excel工作表的单元格中插入或更新文本。现在让我们看看如何使用Python将图像插入工作表中。

  1. 创建一个新的Excel工作簿或使用工作簿类加载一个现有的工作簿。
  2. 使用Worksheet类访问要插入图片的工作表。
  3. 使用Worksheet.getPictures().add(upperLeftRow, upperLeftColumn, fileName)方法插入图片。
  4. 使用Workbook.save(fileName)方法将工作簿保存为.xlsx文件。

以下代码示例显示了如何使用Python创建Excel文件并插入图像。

# create a new XLSX workbookworkbook = Workbook(FileFormatType.XLSX)worksheet = workbook.getWorksheets().get(0)# Insert a string value to a cellworksheet.getCells().get("C2").setValue("Image")# set the 4th row heightworksheet.getCells().setRowHeight(3, 150)# set the C column widthworksheet.getCells().setColumnWidth(3,50)# add a picture to the D4 cellindex = worksheet.getPictures().add(3, 3, "aspose-cells-for-python.png")# get the picture objectpic = worksheet.getPictures().get(index)# save the Excel fileworkbook.save("workbook_with_image.xlsx")

想在Python应用程序中创建Excel吗看看这份Aspose.Cells for Python via Java指南

使用Python在Excel工作表中生成图表

Excel工作表中的图表用于以直方图,金字塔,条形图,甜甜圈等形式直观地表示数据。通过Java的Aspose.Cells for Python支持多种图表类型。以下是在Excel工作表中生成图表的步骤。

  1. 创建一个新的Excel工作簿或使用工作簿类加载一个现有的工作簿。
  2. 访问所需的工作表,并在单元格中添加值(如果工作表已经包含数据,则可选)。
  3. 使用Worksheet.getCharts()方法获取图表集合。
  4. 使用Worksheet.getCharts().add(type,upperLeftRow,upperLeftColumn,lowerRightRow,lowerRightColumn)方法在图表集合中添加一个新图表。
  5. 定义单元格的范围,为图表设置NSeries。
  6. 使用Workbook.save(fileName)方法创建Excel.xlsx文件。

下面的代码示例演示如何使用Python在Excel工作表中生成图表。

# create a new XLSX workbookworkbook = Workbook(FileFormatType.XLSX)# obtaining the reference of the first worksheetworksheets = workbook.getWorksheets()sheet = worksheets.get(0)# adding some sample value to cellscells = sheet.getCells()cell = cells.get("A1")cell.setValue(50)cell = cells.get("A2")cell.setValue(100)cell = cells.get("A3")cell.setValue(150)cell = cells.get("B1")cell.setValue(4)cell = cells.get("B2")cell.setValue(20)cell = cells.get("B3")cell.setValue(50)# get charts in worksheetcharts = sheet.getCharts()# adding a chart to the worksheetchartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5)chart = charts.get(chartIndex)# adding NSeries (chart data source) to the chart ranging from "A1"# cell to "B3"serieses = chart.getNSeries()serieses.add("A1:B3", True)# write the Excel fileworkbook.save("workbook_with_chart.xlsx")

想在Python应用程序中创建Excel吗看看这份Aspose.Cells for Python via Java指南

使用Python在Excel工作表中创建数据透视表

在Excel中创建数据透视表是为了汇总工作表中的大量数据。您可以指定数据透视表中要使用的单元格的范围。以下是通过Java使用Aspose.Cells for Python创建数据透视表的步骤。

  1. 加载Excel文件或使用Workbook类创建一个新文件。
  2. 将数据插入工作表(可选)。
  3. 使用Worksheet.getPivotTables()方法访问数据透视表。
  4. 使用Worksheet.getPivotTables()。add(sourceData,destCellName,tableName)方法添加新的数据透视表。
  5. 配置数据透视表的行,列和数据区域。
  6. 使用Workbook.save(fileName)方法将工作簿另存为.xlsx。

以下代码示例显示了如何使用Python在Excel中创建数据透视表。

# create a new XLSX workbookworkbook = Workbook(FileFormatType.XLSX)# obtaining the reference of the newly added worksheetsheetIndex = workbook.getWorksheets().add()sheet = workbook.getWorksheets().get(sheetIndex)cells = sheet.getCells()# setting the value to the cellscell = cells.get("A1")cell.setValue("Sport")cell = cells.get("B1")cell.setValue("Quarter")cell = cells.get("C1")cell.setValue("Sales")cell = cells.get("A2")cell.setValue("Golf")cell = cells.get("A3")cell.setValue("Golf")cell = cells.get("A4")cell.setValue("Tennis")cell = cells.get("A5")cell.setValue("Tennis")cell = cells.get("A6")cell.setValue("Tennis")cell = cells.get("A7")cell.setValue("Tennis")cell = cells.get("A8")cell.setValue("Golf")cell = cells.get("B2")cell.setValue("Qtr3")cell = cells.get("B3")cell.setValue("Qtr4")cell = cells.get("B4")cell.setValue("Qtr3")cell = cells.get("B5")cell.setValue("Qtr4")cell = cells.get("B6")cell.setValue("Qtr3")cell = cells.get("B7")cell.setValue("Qtr4")cell = cells.get("B8")cell.setValue("Qtr3")cell = cells.get("C2")cell.setValue(1500)cell = cells.get("C3")cell.setValue(2000)cell = cells.get("C4")cell.setValue(600)cell = cells.get("C5")cell.setValue(1500)cell = cells.get("C6")cell.setValue(4070)cell = cells.get("C7")cell.setValue(5000)cell = cells.get("C8")cell.setValue(6430)pivotTables = sheet.getPivotTables()# adding a PivotTable to the worksheetindex = pivotTables.add("=A1:C8", "E3", "PivotTable2")# accessing the instance of the newly added PivotTablepivotTable = pivotTables.get(index)# unshowing grand totals for rows.pivotTable.setRowGrand(False)# dragging the first field to the row area.pivotTable.addFieldToArea(PivotFieldType.ROW, 0)# dragging the second field to the column area.pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1)# dragging the third field to the data area.pivotTable.addFieldToArea(PivotFieldType.DATA, 2)# write the Excel fileworkbook.save("workbook_with_pivot_table.xlsx")

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年7月22日
下一篇 2020年7月22日

相关推荐

发表回复

登录后才能评论