【问题标题】:Dynamically update openpyxl generated charts when sorting columns对列进行排序时动态更新openpyxl生成的图表
【发布时间】:2017-09-01 20:47:52
【问题描述】:

我正在使用 openpyxl 2.4.8 生成一些 excel 文件。我将一些数据填充到一些列中并插入一个绘制该数据的图表。如果我在 excel 中手动执行此操作,如果我使用数据排序方法删除数据点,图表会动态更新。但是,openpyxl 生成的图表是静态的,忽略任何此类排序。 在查看由 excel 生成的图表的 xml 和由 openpyxl 生成的图表时,我看到了很多差异(fx。所有标签在 excel 中都以 'c:' 开头),但没有什么看起来像会自动设置的设置更新内容。我在 excel 中找不到可以打开或关闭它的设置。 我用来生成带有图表的 excel 文件的代码在这里:

import numpy as np
from openpyxl import *
from random import random
from openpyxl.utils.cell import get_column_letter
from openpyxl.chart import (
    LineChart,
    BarChart,
    ScatterChart,
    Reference,
    Series,
)
from openpyxl.drawing.text import CharacterProperties

wb = Workbook()

ws = wb.create_sheet()
ws.title = 'interactiveChart'

num = 9

ws.cell(column=1, row=2, value='X')
ws.cell(column=2, row=2, value='Y')
for i in range(num+1):
    ws.cell(column=1, row=3+i, value=random()*100)
    ws.cell(column=2, row=3+i, value='=A{0}*3+4+ABS(5/(11-A{0}))+ABS(10/(35-    A{0}))+ABS(30/(67-A{0}))'.format(3+i))

textSize = 10
modeChart = ScatterChart()
modeChart.title = 'Resonance'
modeChart.title.tx.rich.p[0].r.rPr = CharacterProperties(sz=textSize*100,     b=True)
modeChart.style = 48
modeChart.x_axis.title = "X"
modeChart.x_axis.title.tx.rich.p[0].r.rPr =     CharacterProperties(sz=textSize*100, b=True)
modeChart.y_axis.title = "Y"
modeChart.y_axis.title.tx.rich.p[0].r.rPr = CharacterProperties(sz=textSize*100, b=True)
modeChart.legend = None

xvalues = Reference(ws, min_col=1, min_row=2, max_row=num+3)
yvalues = Reference(ws, min_col=2, min_row=2, max_row=num+3)
series = Series(yvalues, xvalues, title_from_data=False, title='Resonace')
modeChart.series.append(series)
s1 = modeChart.series[0]
s1.marker.symbol = "diamond"
s1.marker.graphicalProperties.solidFill = "6495ED"
s1.marker.graphicalProperties.line.solidFill = "6495ED"
s1.graphicalProperties.line.noFill = True

modeChart.x_axis.tickLblPos = "low"
modeChart.y_axis.tickLblPos = "low"

modeChart.width = 12
modeChart.height = 7
ws.add_chart(modeChart, "F6")

ws.auto_filter.ref = 'A2:B{}'.format(num+3)

ws = wb.get_sheet_by_name("Sheet")
wb.remove_sheet(ws)
wb.save('aTest.xlsx')

我找不到对这种行为的引用,所以我也不确定我应该寻找什么。

【问题讨论】:

  • 添加您的openpyxl 版本。

标签: python excel charts openpyxl


【解决方案1】:

这不能直接在openpyxl中完成。

openpyxl 是一个文件格式库,而不是 Excel 的替代品。过滤和排序需要通过Excel等应用程序来完成,openpyxl只是维护了相关的元数据。

【讨论】:

  • 我不想用 openpyxl 过滤或排序数据。我正在使用 openpyxl 创建 Excel 文件并在 Excel 中打开它们。然后我使用 Excel 对数据进行过滤和排序。如果我在 excel 中创建了一个图表,则此图表会在过滤数据时更新,但使用 openpyxl 创建的图表在我使用 Excel 过滤数据时不会更新。
  • 无法真正评论 Excel 的功能,但我希望图表能够更新以反映数据的变化,它在我的图表中也是如此。
【解决方案2】:

我找到了解决方案。 Excel图表的xml中有一个标签叫做:<plotVisOnly/>。此标签是在 openpyxl 2.5 中添加的(请参阅 bitbucket 问题:Setting property plotVisOnly for charts)。 感谢friendly openpyxl contributor 帮助我解决了这个问题。 为了使我的 anaconda 安装工作,我首先删除了 openpyxl:

pip uninstall openpyxl

然后我从python.org package index下载了最新的openpyxl版本(>2.5),解压后运行:

python setup.py install

这安装了 >2.5 版本的 openpyxl。我通过运行在 python 中仔细检查了这一点:

import openpyxl
openpyxl.__version__

希望这对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多