python创建excel图表_Python:使用图表创建Excel工作表

这篇博客展示了如何在Python中使用JPype和SmartXLS Java库来创建Excel工作表并添加图表。通过设置数据、公式、图表类型、轴标题、系列颜色等,创建了一个包含多个系列的柱状图,并保存到文件。
摘要由CSDN通过智能技术生成

它有点复杂(和/或邪恶),但类似的东西可以跨平台(包括在Linux下)使用JPype来包装SmartXLSExcel Java库。

此示例使用SmartXLS中的简单图表创建(在Charts/ChartSample.class中)示例。#!/usr/bin/env python

import os

import os.path

import jpype

# or wherever your java is installed

os.environ['JAVA_HOME'] = "/usr/lib64/jvm/default-java"

root = os.path.abspath(os.path.dirname(__file__))

SX_JAR = os.path.join(root, 'SX.jar')

options = [

'-Djava.class.path=%s' % SX_JAR

]

jpype.startJVM(jpype.getDefaultJVMPath(), *options)

WorkBook = jpype.JClass('com.smartxls.WorkBook')

ChartShape = jpype.JClass('com.smartxls.ChartShape')

ChartFormat = jpype.JClass('com.smartxls.ChartFormat')

Color = jpype.JClass('java.awt.Color')

workbook = WorkBook()

workbook.setText(0,1,"Jan")

workbook.setText(0,2,"Feb")

workbook.setText(0,3,"Mar")

workbook.setText(0,4,"Apr")

workbook.setText(0,5,"Jun")

workbook.setText(1,0,"Comfrey")

workbook.setText(2,0,"Bananas")

workbook.setText(3,0,"Papaya")

workbook.setText(4,0,"Mango")

workbook.setText(5,0,"Lilikoi")

for col in range(1, 5 + 1):

for row in range(1, 5 + 1):

workbook.setFormula(row, col, "RAND()")

workbook.setText(6, 0, "Total")

workbook.setFormula(6, 1, "SUM(B2:B6)")

workbook.setSelection("B7:F7")

# auto fill the range with the first cell's formula or data

workbook.editCopyRight()

left = 1.0

top = 7.0

right = 13.0

bottom = 31.0

# create chart with it's location

chart = workbook.addChart(left,top,right,bottom)

chart.setChartType(ChartShape.Column)

# link data source, link each series to columns(true to rows).

chart.setLinkRange("Sheet1!$a$1:$F$6", False)

# set axis title

chart.setAxisTitle(ChartShape.XAxis, 0, "X-axis data")

chart.setAxisTitle(ChartShape.YAxis, 0, "Y-axis data")

# set series name

chart.setSeriesName(0, "My Series number 1")

chart.setSeriesName(1, "My Series number 2")

chart.setSeriesName(2, "My Series number 3")

chart.setSeriesName(3, "My Series number 4")

chart.setSeriesName(4, "My Series number 5")

chart.setTitle("My Chart")

# set plot area's color to darkgray

chartFormat = chart.getPlotFormat()

chartFormat.setSolid()

chartFormat.setForeColor(Color.DARK_GRAY.getRGB())

chart.setPlotFormat(chartFormat)

# set series 0's color to blue

seriesformat = chart.getSeriesFormat(0)

seriesformat.setSolid()

seriesformat.setForeColor(Color.BLUE.getRGB())

chart.setSeriesFormat(0, seriesformat)

# set series 1's color to red

seriesformat = chart.getSeriesFormat(1)

seriesformat.setSolid()

seriesformat.setForeColor(Color.RED.getRGB())

chart.setSeriesFormat(1, seriesformat)

# set chart title's font property

titleformat = chart.getTitleFormat()

titleformat.setFontSize(14*20)

titleformat.setFontUnderline(True)

chart.setTitleFormat(titleformat)

workbook.write("./Chart.xls")

jpype.shutdownJVM()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值