2017-11-29 81 views
0

저는 파이썬 3.6.3을 사용하여 openpyxl (2.4.9)로 일부 엑셀 시트를 작성하고 있습니다. 차트 데이터에서 데이터 레이블을 가져 오는 것은 명확하지 않지만, 데이터 레이블을 형식화하려고하면 상황이 잘못 될 수 있습니다. 내가 할 수 있기를 원하는 것은 자신의 위치를 ​​바꾸고 회전을 바꾸는 것입니다. 누구든지 어떤 아이디어? 파이썬에 조금 신선한 것이므로 어떤 조언도 훌륭합니다.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 여전히 (txPr이 수정 될 수 있지만 위치하지 않음) 작동하지 않습니다

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) 

참고. 분명한 해결 방법은 각 시리즈의 위치를 ​​개별적으로 설정해야합니다.

0

미안하지만, 난 당신이 항상이 물건에 명확하지 않은 OOXML 사양의 한계를 타격하고 두려워 : 는 여기에 내가 무엇을 시도했다입니다. 본질적으로,이 수준에서 openpyxl은 XML 스키마를 Python으로 공개하므로 Excel에서 작성한 비교 가능한 파일과 함께 제공되는 사양 사본을 가장 잘 볼 수 있습니다.

문서화되지 않은 종류의 예 : 차트 라인의 색을 다르게하려면 DrawingML의 관련 부분에 lumOff 앞에 lumMod이 있어야합니다.

이 모든 것을 openpyxl에서 절대 문서화 할 수 없습니다.