没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2020-08-25 14:22:54.183|阅读 594 次
概述:在过去的几年中,Python已成为主要的编程语言之一。本文旨在将Python和电子表格自动化结合在一起,向您展示如何使用Python创建Excel XSL / XLSX文件。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
Aspose.Cells for Python via Java是一个功能强大但易于使用的电子表格处理API,可让您使用Python在应用程序中实现电子表格自动化。您可以用几行代码创建新的Excel文件,以及更新和转换现有的电子表格文档。
在过去的几年中,Python已成为主要的编程语言之一。Python的实用性和受欢迎程度极大地发展了Python爱好者社区。另一方面,电子表格自动化使从Web或桌面应用程序中保存,组织和播放大量数据变得更加容易。
本文旨在将Python和电子表格自动化结合在一起,向您展示如何使用Python创建Excel XSL / XLSX文件。此外,您还将学习如何使用Python以编程方式在Excel工作表中插入数据,图像,图表和表格。
点击下载Aspose.Cells for Python via Java
首先,通过Java使用Aspose.Cells for Python创建一个简单的Excel XLSX文件。以下是执行此操作的步骤:
下面的代码示例演示如何使用Python创建Excel XLSX文件。
# create a new XLSX workbook
wb = Workbook(FileFormatType.XLSX)
# insert value in the cells
wb.getWorksheets().get(0).getCells().get("A1").putValue("Hello World!")
# save workbook as .xlsx file
wb.save("workbook.xlsx")
在上一个示例中,您从头创建了一个新的Excel XLSX文件。但是,在某些情况下,您需要更新现有Excel文件的内容。在这种情况下,可以通过提供工作簿构造函数的路径来加载Excel文件。用于访问工作表和单元格的其余方法将保持不变。
以下代码示例显示了如何使用Python更新Excel文件。
# create a new XLSX workbook
wb = Workbook("workbook.xlsx")
# insert value in the cells
wb.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 file
wb.save("workbook-updated.xlsx")
在前面的两个示例中,您已经了解了如何在Excel工作表的单元格中插入或更新文本。现在让我们看看如何使用Python将图像插入工作表中。
以下代码示例显示了如何使用Python创建Excel文件并插入图像。
# create a new XLSX workbook
workbook = Workbook(FileFormatType.XLSX)
worksheet = workbook.getWorksheets().get(0)
# Insert a string value to a cell
worksheet.getCells().get("C2").setValue("Image")
# set the 4th row height
worksheet.getCells().setRowHeight(3, 150)
# set the C column width
worksheet.getCells().setColumnWidth(3,50)
# add a picture to the D4 cell
index = worksheet.getPictures().add(3, 3, "aspose-cells-for-python.png")
# get the picture object
pic = worksheet.getPictures().get(index)
# save the Excel file
workbook.save("workbook_with_image.xlsx")
Excel工作表中的图表用于以直方图,金字塔,条形图,甜甜圈等形式直观地表示数据。通过Java的Aspose.Cells for Python支持多种图表类型。以下是在Excel工作表中生成图表的步骤。
下面的代码示例演示如何使用Python在Excel工作表中生成图表。
# create a new XLSX workbook
workbook = Workbook(FileFormatType.XLSX)
# obtaining the reference of the first worksheet
worksheets = workbook.getWorksheets()
sheet = worksheets.get(0)
# adding some sample value to cells
cells = 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 worksheet
charts = sheet.getCharts()
# adding a chart to the worksheet
chartIndex = 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 file
workbook.save("workbook_with_chart.xlsx")
在Excel中创建数据透视表是为了汇总工作表中的大量数据。您可以指定数据透视表中要使用的单元格的范围。以下是通过Java使用Aspose.Cells for Python创建数据透视表的步骤。
以下代码示例显示了如何使用Python在Excel中创建数据透视表。
# create a new XLSX workbook
workbook = Workbook(FileFormatType.XLSX)
# obtaining the reference of the newly added worksheet
sheetIndex = workbook.getWorksheets().add()
sheet = workbook.getWorksheets().get(sheetIndex)
cells = sheet.getCells()
# setting the value to the cells
cell = 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 worksheet
index = pivotTables.add("=A1:C8", "E3", "PivotTable2")
# accessing the instance of the newly added PivotTable
pivotTable = 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 file
workbook.save("workbook_with_pivot_table.xlsx")
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
BarTender 标签管理系统,正是帮助企业轻松实现 GS1 标准化标签设计、编码生成与信息联动的强大工具。
Parasoft C/C++test 是一款功能强大的 C/C++ 软件测试工具,集成了静态代码分析、单元测试、集成测试和覆盖率分析等功能,单元测试作为其关键功能之一,为了适配多样化的目标部署环境,C/C++test 设计了灵活的测试结果收集机制。通过Socket通讯方式来收集单元测试结果,从而扩展其测试覆盖范围与应用场景。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营