2017-11-29 195 views
0

私はPython 3.6.3を使って、openpyxl(2.4.9)でいくつかのExcelシートを作成しています。グラフ化されたデータのデータラベルを取得することは明らかではありませんでしたが、データラベルをフォーマットしようとすると状況が悪くなります。私ができることを望みたいのは、彼らのポジションを変えたり、自分のローテーションを変えたりすることです。誰にでもアイデアはありますか?私は少しPythonの新鮮ですので、任意のアドバイスが素晴らしいだろう。openpyxlでのチャートデータラベルの書式設定

from openpyxl.chart import LineChart, Reference, Series 
from openpyxl.chart.label import DataLabelList 
from openpyxl.chart.text import RichText 
from openpyxl.drawing.text import RichTextProperties 

#this is the only way I found to set text properties which didnt give an error 
vertical = RichTextProperties(rot = 270) 
labelvertical = RichText(bodyPr = vertical) 


chart1 = LineChart() 

# setup and append the first series 
values = Reference(dprsheet, min_col=5, min_row=4, max_row=34) 
series = Series(values, title="Series 1") 
chart1.append(series) 

# setup and append the second series 
values = Reference(dprsheet, min_col=8, min_row=4, max_row=34) 
series = Series(values, title="Series 2") 
chart1.append(series) 

dates = Reference(dprsheet, min_col=2, min_row=4, max_row=34) 

chart1.set_categories(dates) 

s1 = chart1.series[0] 
s1.graphicalProperties.line.solidFill = 'FF0000' 
s1.graphicalProperties.line.width = 25000 
s1.dLbls = DataLabelList() 
s1.dLbls.showVal = True 

## s1.dLbls.dLblPos = 't' 
## if ^this^ line isn't commented then ALL images in the sheet are removed by excel upon opening 
## s1.dLbls.txPr = labelvertical 
## if ^this^ line isn't commented then ALL images in the sheet are removed by excel upon opening 
s2 = chart1.series[1] 
s2.graphicalProperties.line.solidFill = '000000' 
s2.graphicalProperties.line.width = 25000 

essheet.add_chart(chart1, 'B35') 

答えて

1

最後に、軸ラベルを変更する方法を試してみましたが、これはフォントの段落プロパティや配置のためのボディプロパティのいずれかを使用して変更する必要があります。 chart.dataLabels.dLblPosがまだ動作していないこと

from openpyxl.chart import LineChart, Reference, Series 
from openpyxl.chart.label import DataLabelList 
from openpyxl.chart.text import RichText 
#additional imports needed for the solution: 
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties 

chart1 = LineChart() 
# setup and append the first series 
values = Reference(dprsheet, min_col=5, min_row=4, max_row=34) 
series = Series(values, title="Series 1") 
chart1.append(series) 
# setup and append the second series 
values = Reference(dprsheet, min_col=8, min_row=4, max_row=34) 
series = Series(values, title="Series 2") 
chart1.append(series) 

dates = Reference(dprsheet, min_col=2, min_row=4, max_row=34) 
chart1.set_categories(dates) 

#create label styling 
axis = CharacterProperties(sz=800) 
rot = openpyxl.drawing.text.RichTextProperties(vert='vert270') 

#set axis label styles 
chart1.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)], bodyPr=rot) 
chart1.y_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)]) 

#set data labels and styles 
s1 = chart1.series[0] 
s1.dLbls = DataLabelList() 
s1.dLbls.showVal = True 
s1.dLbls.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)], bodyPr=rot) 

は注意(TXPRが改正されたが位置ではないことができます)。明らかに回避するには、各シリーズごとに個別にポジションを設定する必要があります。

0

申し訳ありませんが、私はあなたが常にこのようなものに非常に明確ではありませんOOXML仕様の限界を打っている怖い: は、ここで私が試したものです。基本的に、このレベルでは、openpyxlはXMLスキーマをPythonに公開しています。そのため、Excelで作成されたファイルと比較して仕様のコピーを作成するのが最適です。

書類に記載されていない種類の例:チャートラインの色を変えたい場合は、の前に、lumModが必要です。

これをすべてopenpyxlで文書化することはできません。