翻譯|使用教程|編輯:李顯亮|2020-08-25 14:22:54.183|閱讀 587 次
概述:在過去的幾年中,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@fc6vip.cn