简体   繁体   English

如何从 Excel 图表图例中删除系列 - Python Openpyxl

[英]How to Remove Series from Excel Chart Legend - Python Openpyxl

I am creating a custom chart style using a ScatterChart from openpyxl v3.0.7.我正在使用 openpyxl v3.0.7 中的 ScatterChart 创建自定义图表样式。 I want to show a legend only containing certain series within the chart, as the rest are only needed for structuring the underlying chart and not pertinent to the data relevant to a standard consumer.我想在图表中显示仅包含某些系列的图例,因为 rest 仅用于构建基础图表,与标准消费者相关的数据无关。

Here's an example of the chart code:这是图表代码的示例:

import openpyxl as xl

wb = xl.workbook.Workbook()
sh = wb['Sheet']
data = [(1, 2), (2, 3)]
for row in data:
  sh.append(row)

hideCnt = 0
serName = 'Test'
xVals = xl.chart.Reference(sh, min_row=1, min_col=1, max_row=2)
yVals = xl.chart.Reference(sh, min_row=1, min_col=2, max_row=2)
ser = xl.chart.Series(yVals, xVals, title=serName)
hideCnt += 1

ch = xl.chart.ScatterChart()
ch.title = 'Chart-1'
ch.series.append(ser)
ch.legend.legendPos = 'b'

entries = []
for i in range(hideCnt):
  entry = xl.chart.legend.LegendEntry(idx=i, delete=1)
  entries.append(entry)
ch.legend.legendEntry = entries

sh.add_chart(ch, 'A3')
wb.save(filename = 'test.xlsx')

This generates an excel output as such: Chart Example w/ Legend (series remain)这会生成一个 excel output 如下:图表示例 w/图例(系列保留)

The series remains in the Legend of the chart, despite the delete=1 tag on the legendEntry for series of idx=0 .尽管idx=0系列的 legendEntry 上有delete=1标记,但该系列仍保留在图表的图例中。 I have looked elsewhere and found no solution that works for deleting these series from the legend using openpyxl.我在别处看了看,没有找到适用于使用 openpyxl 从图例中删除这些系列的解决方案。 Per a similar question, How to remove a legend name in openpyxl , I was led to this post: LegendEntry does not contain delete element when serialised , last updated a year ago.根据一个类似的问题, 如何在 openpyxl 中删除图例名称,我被引导到这篇文章: LegendEntry does not contain delete element when serialized ,最后一次更新是一年前。

Here is the resultant XML in 'test.xlsx\xl\charts\chart1.xml' from my code:这是我的代码中“test.xlsx\xl\charts\chart1.xml”中生成的 XML:

<chartSpace xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns="http://schemas.openxmlformats.org/drawingml/2006/chart">
 <chart>
  <title>
   <tx>
    <rich>
     <a:bodyPr/>
     <a:p>
      <a:pPr>
       <a:defRPr/>
      </a:pPr>
      <a:r>
       <a:t>Chart-1</a:t>
      </a:r>
     </a:p>
    </rich>
   </tx>
  </title>
  <plotArea>
   <scatterChart>
    <ser>
     <idx val="0"/>
     <order val="0"/>
     <tx>
      <v>Test</v>
     </tx>
     <spPr>
      <a:ln>
       <a:prstDash val="solid"/>
      </a:ln>
     </spPr>
     <marker>
      <symbol val="none"/>
      <spPr>
       <a:ln>
        <a:prstDash val="solid"/>
       </a:ln>
      </spPr>
     </marker>
     <xVal>
      <numRef>
       <f>'Sheet'!$A$1:$A$2</f>
      </numRef>
     </xVal>
     <yVal>
      <numRef>
       <f>'Sheet'!$B$1:$B$2</f>
      </numRef>
     </yVal>
    </ser>
    <axId val="10"/>
    <axId val="20"/>
   </scatterChart>
   <valAx>
    <axId val="10"/>
    <scaling>
     <orientation val="minMax"/>
    </scaling>
    <axPos val="l"/>
    <majorGridlines/>
    <majorTickMark val="none"/>
    <minorTickMark val="none"/>
    <crossAx val="20"/>
   </valAx>
   <valAx>
    <axId val="20"/>
    <scaling>
     <orientation val="minMax"/>
    </scaling>
    <axPos val="l"/>
    <majorGridlines/>
    <majorTickMark val="none"/> 
    <minorTickMark val="none"/>
    <crossAx val="10"/>
   </valAx>
  </plotArea>
  <legend>
   <legendPos val="b"/>
   <legendEntry>
    <idx val="0"/>
    <delete val="1"/>
   </legendEntry>
  </legend>
  <plotVisOnly val="1"/>
  <dispBlanksAs val="gap"/>
 </chart>
</chartSpace>

The resultant XML does actually have the correct delete tags associated with the legendEntry at idx=0, which correlates to the correct chart series, 'Test', w/ series <idx val="0"/> .生成的 XML 实际上确实具有与 idx=0 处的 legendEntry 关联的正确删除标签,这与正确的图表系列“测试”相关,w/系列<idx val="0"/> However, the end result is the same.然而,最终的结果是一样的。 If I save the file after opening in Excel then the XML changes and all of the legendEntry tags are removed.如果我在 Excel 中打开后保存文件,则 XML 会更改并且所有 legendEntry 标记都将被删除。

XML legend excerpt after saving from Excel:从 Excel 保存后的 XML 图例摘录:

<c:legend>
 <c:legendPos val="b"/>
 <c:overlay val="0"/>
</c:legend>

In addition to removing the legendEntry elements, there seems to be a prefix added to all elements that were previous left without a namespace (from the original openpyxl output).除了删除 legendEntry 元素之外,似乎还有一个前缀添加到之前没有命名空间的所有元素(来自原始的 openpyxl 输出)。

http://schemas.openxmlformats.org/drawingml/2006/chart http://schemas.openxmlformats.org/drawingml/2006/chart

I have attempted a workaround by loading the modified workbook w/ adjusted namespaces into openpyxl and changing the charts after the fact, but I have had no luck accessing existing charts from a saved workbook.我尝试了一种解决方法,将带有调整后的命名空间的修改后的工作簿加载到 openpyxl 并在事后更改图表,但我没有运气从保存的工作簿中访问现有图表。

Does this seem related to the namespace difference between the Excel version I'm running and the output generated by openpyxl?这似乎与我正在运行的 Excel 版本和 openpyxl 生成的 output 之间的命名空间差异有关吗? If so, is there a way for me to modify the namespaces to adjust accordingly?如果是这样,我有没有办法修改命名空间以进行相应调整? If not, what am I doing wrong?如果没有,我做错了什么?

It looks like you may need to add a c:numCache node (under c:numRef node) to get the legend entry deletion to be recognized.看起来您可能需要添加一个 c:numCache 节点(在 c:numRef 节点下)才能识别图例条目删除。 Based on your xml above, that probably will be numCache node under numRef.根据您上面的 xml,这可能是 numRef 下的 numCache 节点。

I ran into the same issue with Microsoft's OpenXml library for .NET.我在使用 Microsoft 的 .NET 的 OpenXml 库时遇到了同样的问题。 So this issue applies to more than just openpqxl but to the OpenXml format in general.所以这个问题不仅适用于 openpqxl,而且适用于一般的 OpenXml 格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM