对列进行排序时动态更新 openpyxl 生成的图表
Dynamically update openpyxl generated charts when sorting columns
我正在使用 openpyxl 2.4.8 生成一些 excel 文件。我将一些数据填充到一些列中,然后插入绘制该数据的图表。如果我在 excel 中手动这样做,如果我使用数据排序方法删除数据点,图表会动态更新。但是,openpyxl 生成的图表是静态的,忽略任何此类排序。
在查看由 excel 生成的图表和由 openpyxl 生成的图表的 xml 时,我看到了很多差异(fx。所有标签都以 'c:' 在 excel),但没有任何东西看起来像是会自动更新内容的设置。我在 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 中完成。
openpyxl 是一个文件格式库,不能替代 Excel。过滤和排序需要通过应用程序完成,例如Excel,openpyxl只是维护相关的元数据。
我找到了解决方案。在 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__
希望这对某人有所帮助!
我正在使用 openpyxl 2.4.8 生成一些 excel 文件。我将一些数据填充到一些列中,然后插入绘制该数据的图表。如果我在 excel 中手动这样做,如果我使用数据排序方法删除数据点,图表会动态更新。但是,openpyxl 生成的图表是静态的,忽略任何此类排序。 在查看由 excel 生成的图表和由 openpyxl 生成的图表的 xml 时,我看到了很多差异(fx。所有标签都以 'c:' 在 excel),但没有任何东西看起来像是会自动更新内容的设置。我在 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 中完成。
openpyxl 是一个文件格式库,不能替代 Excel。过滤和排序需要通过应用程序完成,例如Excel,openpyxl只是维护相关的元数据。
我找到了解决方案。在 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__
希望这对某人有所帮助!