gpt4 book ai didi

python - 如何使用 Python-pptx 在 PowerPoint 中反转聚类条形图的类别顺序?

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:40 25 4
gpt4 key购买 nike

我正在尝试使用 Python-pptx 生成聚类条形图。但是图表上出现的类别顺序与数据表中的顺序相反。

在 PowerPoint 中,勾选类别轴选项中的“类别倒序”即可解决问题。我已经搜索了一段时间,但在 Python 代码中找不到等效的属性。非常感谢任何帮助或建议。

最佳答案

@Boosted_d16 指出,API 还不直接支持此功能。看起来这可以使用变通功能相当简单地完成。首先,我们需要识别底层 XML 中的差异,然后相应地操作我们的输出 XML。

这里是 BAR_CLUSTERED 图表的相关部分,作为 pptx 的默认值,这是指它的 category_axis:

  <c:catAx>
<c:axId val="-2068027336"/>
<c:scaling>
<c:orientation val="maxMin"/>
</c:scaling>

如果我们在 PowerPoint 应用程序中手动将其修改为反向顺序的类别,它将看起来像这样:

  <c:catAx>
<c:axId val="-2068027336"/>
<c:scaling>
<c:orientation val="minMax"/>
</c:scaling>

所以唯一的变化是 /c:scaling/c:orientation[0] 元素,需要给它赋值 "minMax" 而不是 "maxMin"。我们可以通过将对轴的引用传递给辅助函数来做到这一点,如下所示:

def set_reverse_categories(axis):
"""
workaround function that replicates the "Categories in Reverse Order" UI option in PPT
"""
ele = axis._element.xpath(r'c:scaling/c:orientation')[0]
ele.set("val", "maxMin")

示例输出

左边是类别轴反转的图表,右边是默认输出。

enter image description here

示例用法

此程序将使用上面屏幕截图中的两张幻灯片创建演示文稿。请注意,您可能需要更改布局索引。

from pptx import Presentation
from pptx.enum.chart import XL_CHART_TYPE
from pptx.chart.data import CategoryChartData
from pandas import DataFrame as DF
p = Presentation()
# Create some data to be used in the chart
series_names = ["A","B","C","D"]
cat_names = ["cat 1"]
data = {
cat_names[0]: [.10, .20, .30, .40]
}
df = DF(data, series_names, cat_names)
cd = CategoryChartData()
cd.categories = df.index
for name in df.columns:
data = df[name]
cd.add_series(name, data, '0%')

layout = p.slide_layouts[6] # MODIFY AS NEEDED, 6 is the index of my "Blank" slide template.

# Create two charts, one reversed and one not reversed on the Category Axis
for reverse in (True, False):
slide = p.slides.add_slide( layout )
shape = slide.shapes.add_chart(XL_CHART_TYPE.BAR_CLUSTERED, 0, 0, 9143301, 6158000, cd)
cht = shape.chart
plot = cht.plots[0]
plot.has_data_labels = False
if reverse:
set_reverse_categories(cht.category_axis)

p.save(r'c:\debug\ppt_chart.pptx')

注意:这也会在视觉上影响图表 w/r/t“交叉点”,并且水平/值轴现在显示在图表顶部。您需要单独进行调整。 pptx API 不直接支持这一点,但也可以通过变通函数实现:

def set_axis_crosses_at(cht, index, position_at):
"""
cht: chart
index: string 'value' or 'category' -- which axis to be adjusted
position_at: 'max, 'autoZero', or int representing category index for Crosses At.
"""
ns = "{http://schemas.openxmlformats.org/drawingml/2006/chart}"
axes = {'value': cht.value_axis, 'category': cht.category_axis}
axis = axes.get(index, None)
if not axis:
return
# probably should throw error here
ax_ele = axis._element
crosses = ax_ele.xpath(r'c:crosses')[0]
scaling = ax_ele.xpath(r'c:scaling')[0]
if position_at in ('max', 'autoZero'):
crosses.set('val', f'{position_at}')
return
elif isinstance(position_at, int):
ax_ele.remove(crosses)
if len(ax_ele.xpath(r'c:auto')) > 0:
ax_ele.remove(ax_ele.xpath(r'c:auto')[0])
# crossesAt:
if len(ax_ele.xpath(r'c:crossesAt')) == 0:
crossesAt = etree.SubElement(ax_ele, f'{ns}crossesAt')
else:
crossesAt = ax_ele.xpath(r'c:crossesAt')[0]
crossesAt.set('val', f'{position_at}')

示例输出:

enter image description here

关于python - 如何使用 Python-pptx 在 PowerPoint 中反转聚类条形图的类别顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56483775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com