2010-02-14 40 views
3

是否有任何模塊用於在Python中創建帶有嵌入圖表的Excel圖表? The modules mentioned in this question似乎沒有這種能力。Python:使用圖表創建Excel工作表

我更喜歡可以在Ubuntu下工作的通用模塊,而不是依賴於Windows的模塊。

編輯:我也很喜歡在創建的圖表中嵌入圖像的方法,因爲我可以在外部程序中創建圖表並將它們放置在正確的表單中。

感謝,

亞當

回答

3

這是一個有點複雜(和/或邪惡),但類似的東西將工作跨平臺(包括在Linux下)使用JPype包裝SmartXLS Excel 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() 
+0

這可以使用Java直接完成,對吧? –

+0

是的,但是,這將允許您以編程方式從現有Python代碼「直接」創建Excel圖表(對於某些直接值;) –

5

在Windows上,你需要使用pywin32和COM。在* x盒子上,您可能會發現Iron Python,Mono和爲.NET編寫的Excel操作庫的組合可以完成這項工作。無論哪種情況,祝你好運。

+0

+1謝謝。我想我會在外部程序中創建我的圖表並將它們嵌入爲圖像。 –

3

我最近找到xlsxwriter。這是我找到的功能最強大的xlsx python模塊,可以與圖表和圖形一起使用。它也不需要任何非標準的Python模塊,可以在任何類型的盒子上工作。無需安裝Windows或安裝圖表軟件。