Adding labels to ScatterChart in openpyxl

巧了我就是萌 提交于 2019-12-11 17:35:33

问题


I have wrote this simple code to test things out for a more complicated one:

import sys

from openpyxl import load_workbook
from openpyxl.chart import Reference, ScatterChart, Series
from openpyxl.chart.label import DataLabel, DataLabelList
from openpyxl.chart.marker import Marker
from openpyxl.chart.shapes import GraphicalProperties
from openpyxl.drawing.colors import ColorChoice

wb = load_workbook(sys.argv[1])
for ws in wb:
    chart = ScatterChart(scatterStyle='smoothMarker')
    for x in range(2, 192):
        labels = Reference(ws, min_col=1, max_col=1, min_row=x, max_row=x)
        xaxis = Reference(ws, min_col=2, max_col=2, min_row=x, max_row=x)
        data = Reference(ws, min_col=3, max_col=3, min_row=x, max_row=x)
        s = Series(xaxis, data, title=ws['A' + str(x)].value)
        sp_color = ColorChoice(prstClr=(str(ws['A' + str(x)].value)))
        sp_sppr = GraphicalProperties(solidFill=sp_color)
        s.marker = Marker(symbol=('circle'), size=20, spPr=sp_sppr)
        s.marker.spPr.ln.noFill = True
        s.spPr.ln.noFill = True
        slabel = list()  #EDITED
        for x in labels.cells:  #EDITED
             slabel.append(ws[x].value)  #EDITED
        s.labels = DataLabelList(dLbl=slabel, dLblPos='bestFit')
        chart.series.append(s)
    chart.dataLabels = (DataLabelList(showLegendKey=True))
    ws.add_chart(chart, 'D1')
wb.save(sys.argv[1])

the file use as sys.argv[1] is like that (technically it's just the color list for prstClr of ColorChoice with x += 10 and y = x):

Color   x   y
beige   0   0
forestGreen 10  10
dkGoldenrod 20  20
lightPink   30  30
slateGrey   40  40

the line s.labels = DataLabelList(dLbl=slabel, dLblPos='bestFit') outputs the error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 57, in _convert
    value = expected_type(value)
ValueError: invalid literal for int() with base 10: 'beige'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 57, in _convert
    value = expected_type(value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/chart/label.py", line 100, in __init__
    self.idx = idx
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/nested.py", line 36, in __set__
    super(Nested, self).__set__(instance, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 69, in __set__
    value = _convert(self.expected_type, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 59, in _convert
    raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'int'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "csv/test.py", line 37, in <module>
    s.labels = DataLabelList(dLbl=slabel, dLblPos='bestFit')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/chart/label.py", line 128, in __init__
    self.dLbl = dLbl
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/sequence.py", line 27, in __set__
    seq = [_convert(self.expected_type, value) for value in seq]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/sequence.py", line 27, in <listcomp>
    seq = [_convert(self.expected_type, value) for value in seq]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/openpyxl/descriptors/base.py", line 59, in _convert
    raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'openpyxl.chart.label.DataLabel'>

EDIT: I used a list as Charlie pointed it out without more success, this list should contains the same amount of items with the label names of each.
Either slabel.append(ws[x].value) or slabel.append(x) output the same error, what should that list contains?

Versions:
- openpyxl 2.4.8
- python 3.6
- macOS Siera 10.12.6

Regards


回答1:


Sequence is a descriptor used when writing classes you're using it incorrectly. You need to provide a Python sequence (list, tuple, etc.)



来源:https://stackoverflow.com/questions/45591823/adding-labels-to-scatterchart-in-openpyxl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!