在openpyxl中向ScatterChart添加标签

时间:2017-08-09 13:17:19

标签: python openpyxl

我已经编写了这个简单的代码来测试更复杂的代码:

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])

文件用作sys.argv[1]就像那样(从技术上来说,它只是ColorChoice的prstClr的color list,其中x + = 10且y = x):

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

s.labels = DataLabelList(dLbl=slabel, dLblPos='bestFit')输出错误:

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'>

编辑:我使用了一个列表,因为查理指出它没有更多的成功,这个列表应该包含相同数量的项目,每个项目的标签名称。
slabel.append(ws[x].value)slabel.append(x)输出相同的错误,该列表应包含哪些内容?

  

版本:
   - openpyxl 2.4.8
   - python 3.6
   - macOS Siera 10.12.6

此致

1 个答案:

答案 0 :(得分:0)

Sequence是在编写错误使用它的类时使用的描述符。您需要提供Python序列(列表,元组等)