如何将组合图拆分为在openpyxl中分隔的单个图

How to split the combined graph into a single graph separated in openpyxl

需要显示通过将 X 轴中的每一行放在设置为 Y 轴的列上绘制的图形。这是我从 Excel sheet 导入并显示在单独的 Excel sheet.

中的数据

from openpyxl import Workbook
from openpyxl.chart import (
    Reference,
    BarChart3D
)

wb = Workbook()
fileItemPath = os.path.join(os.path.dirname(__file__), "Data.xlsx")
print('Input_FilePath: {}'.format(fileItemPath))
wb = openpyxl.load_workbook(fileItemPath)
sheet = wb['TMO_Reliability_Stats_Combined']

# This part of the code assigning to data causing the whole trouble...
data = Reference(sheet, min_col=2, min_row=2, max_col=2, max_row=6)

titles = Reference(sheet, min_col=2, max_col=2, min_row=1, max_row=1)
chart = BarChart3D()
chart.title = "3D Bar Chart"
chart.add_data(data=data, titles_from_data=True, from_rows=True)
chart.set_categories(titles)
sheet.add_chart(chart, "A10")
wb.save("bar3d.xlsx")

在上面的代码中,我无法分配正确的值 data = Reference(sheet, min_col=2, min_row=2, max_col=2, max_row=6)

实际结果:显示组合图而不是单个图。

预期结果:单个图需要单独绘制。组合需要消除如截图所示

我不确定您为什么会收到此错误,但我相信这与您以一种并非完全有意的方式使用 openpyxl 条形图有关。它可以很容易地与 Excel 有关。深入研究这个模块的 openpyxl 源代码还没有给我答案。

但是,更改 data 以包含第一列确实可以解决您的问题。我可以获得 2021 的单独图表的唯一方法是移动数据和用例,使它们成为单独的一对。虽然如果您有多年的经验,这不是一个理想的解决方案,但它确实可以按您的预期工作。

from openpyxl import Workbook
from openpyxl.chart import (
    Reference,
    BarChart3D
)
import os
import openpyxl

wb = Workbook()
fileItemPath = os.path.join(os.path.dirname(__file__), "Data.xlsx")
print('Input_FilePath: {}'.format(fileItemPath))
wb = openpyxl.load_workbook(fileItemPath)
sheet = wb['Sheet1']

# Copying/moving 2021 data
for row in sheet.iter_rows(min_col=1, max_col=5):
    # Copy data
    colA = row[0].value
    colC = row[2].value

    # Delete colC
    row[2].value = None

    # Paste data
    row[3].value = colA
    row[4].value = colC

# 2020 chart
data = Reference(sheet, min_col=1, min_row=2, max_col=2, max_row=6)
titles = Reference(sheet, min_col=2, min_row=1)
chart = BarChart3D()
chart.title = "3D Bar Chart"
chart.add_data(data=data, titles_from_data=True, from_rows=True)
chart.set_categories(titles)
sheet.add_chart(chart, "A10")

# 2021 chart
data = Reference(sheet, min_col=4, min_row=2, max_col=5, max_row=6)
titles = Reference(sheet, min_col=5, min_row=1)
chart = BarChart3D()
chart.title = "3D Bar Chart"
chart.add_data(data=data, titles_from_data=True, from_rows=True)
chart.set_categories(titles)
sheet.add_chart(chart, "J10")

# Saving
wb.save("bar3d.xlsx")

输出的条形图:

from openpyxl.chart import (
    Reference,
    BarChart3D
)
from openpyxl.styles.numbers import FORMAT_PERCENTAGE

fileItemPath = os.path.join(os.path.dirname(__file__), "Data.xlsx")

def movCopyCell(sheet):
    for (index, row) in enumerate(sheet.iter_rows(min_col=1, max_col=8)):
        # Copy data
        colA = row[0].value
        colC = row[2].value
        colD = row[0].value
        colE = row[3].value

        # Delete 
        row[2].value = None
        row[3].value = None

        # Paste data
        row[3].value = colA
        # row[3].number_format = FORMAT_PERCENTAGE
        row[4].value = colC
        # row[4].number_format = FORMAT_PERCENTAGE
        row[6].value = colD
        # row[6].number_format = FORMAT_PERCENTAGE
        row[7].value = colE
        # row[7].number_format = FORMAT_PERCENTAGE

def initalizeChartDetails(i, sheet):
    data = Reference(sheet, min_col=(1 + (i - 1) * 3), min_row=2, max_col=((i + 1) + (i - 1) * 2), max_row=6)
    titles = Reference(sheet, min_col=((i+1) + (i-1)*2), min_row=1)
    chart = BarChart3D()
    chart.title = "3D Bar Chart"
    chart.add_data(data=data, titles_from_data=True, from_rows=True)
    chart.set_categories(titles)
    if (i % 2 == 0):
        columnName = chr(72) + f'{i * 20}'
    else:
        columnName = chr(65) + f'{i * 20}'
    sheet.add_chart(chart, columnName)

wb = openpyxl.load_workbook(fileItemPath)
sheet = wb['TMO_Reliability_Stats_Combined']
movCopyCell(sheet)
for i in range(1, 4, 1):
    initalizeChartDetails(i, sheet)
else:
    # save the file
    wb.save("bar3d.xlsx")