import openpyxl
from openpyxl.chart import Reference, BarChart, LineChart
from openpyxl.chart.data_source import AxDataSource, NumRef, StrRef
from openpyxl.chart.axis import ChartLines
path = 'C:\\temp\\test.xlsx'
# 차트를 읽는 함수 (bar와 line이 섞여 있는 차트의 경우)
def read_composite_chart(chart):
bar_chart = None
line_chart = None
for ch in chart._charts:
if isinstance(ch, BarChart):
bar_chart = ch
elif isinstance(ch, LineChart):
line_chart = ch
return bar_chart, line_chart
# 차트의 x축값, 카테고리를 출력
def print_chart_categories(chart):
# Access all chart types within the composite chart
bar_chart, line_chart = read_composite_chart(chart)
# Print Bar Chart categories
if bar_chart and bar_chart.series[0].cat:
print("Bar Chart Categories:")
for label in bar_chart.series[0].cat.strRef.strCache.pt:
# 사용할 수 있는 변수 확인 idx, v
print(vars(label))
# 확인한 변수를 가지고 출력
print(label.idx)
print(label.v)
# Print Line Chart categories
# if line_chart and line_chart.series[0].cat:
# print("Line Chart Categories:")
# for label in line_chart.series[0].cat.strRef.strCache.pt:
# print(label.v)
# 차트의 카테고리 범위 변경.
def update_chart_categories(chart, sheet, new_range):
# Access all chart types within the composite chart
bar_chart, line_chart = read_composite_chart(chart)
# Update Bar Chart categories
if bar_chart:
bar_chart.series[0].cat = AxDataSource(strRef=StrRef(f=new_range))
print("Bar Chart categories updated.")
# Update Line Chart categories
if line_chart:
line_chart.series[0].cat = AxDataSource(strRef=StrRef(f=new_range))
print("Line Chart categories updated.")
# 엑셀 파일 열기
wb = openpyxl.load_workbook(path)
sheet = wb['sheet1'] # 원하는 시트 이름으로 변경
# 차트 객체 가져오기 (첫 번째 차트로 가정)
chart = sheet._charts[0]
# 새로운 카테고리 범위 설정 (예: C18:C30)
# 엑셀에서 해당 범위 드레그로 확인해서 복붙하는게 좋음.
new_range = "'Sheet1'!$C$18:$C$20"
update_chart_categories(chart, sheet, new_range)
wb.save(path)
# 엑셀 파일 닫기
wb.close()
import openpyxl
from openpyxl.chart import Reference, BarChart, LineChart
def read_composite_chart(chart):
bar_chart = None
line_chart = None
for ch in chart._charts:
if isinstance(ch, BarChart):
bar_chart = ch
elif isinstance(ch, LineChart):
line_chart = ch
return bar_chart, line_chart
def print_chart_series_and_categories(chart):
# Access all chart types within the composite chart
bar_chart, line_chart = read_composite_chart(chart)
# Print Bar Chart series titles and categories
if bar_chart:
print("Bar Chart Series Titles:")
for i, series in enumerate(bar_chart.series):
barTitle = series.tx.v if series.tx and series.tx.v else series.title
bar_val_ref = series.val.numRef.f if series.val and series.val.numRef else None
print(f"title: {barTitle}")
print(f"Bar Chart Series Values Reference: {bar_val_ref}")
# Print Bar Chart categories
if bar_chart.series[0].cat:
print("Bar Chart Categories:")
for label in bar_chart.series[0].cat.strRef.strCache.pt:
print(label.v)
# Print Line Chart series titles and categories
if line_chart:
print("Line Chart Series Titles:")
for i, series in enumerate(line_chart.series):
lineTitle = series.tx.v if series.tx and series.tx.v else series.title
line_val_ref = series.val.numRef.f if series.val and series.val.numRef else None
print(f"title: {lineTitle}")
print(f"Line Chart Series Values Reference: {line_val_ref}")
# Print Line Chart categories
if line_chart.series[0].cat:
print("Line Chart Categories:")
for label in line_chart.series[0].cat.strRef.strCache.pt:
print(label.v)
# 엑셀 파일 열기
wb = openpyxl.load_workbook('C:\\temp\\test.xlsx')
sheet = wb['Sheet1'] # 원하는 시트 이름으로 변경
# 차트 객체 가져오기 (첫 번째 차트로 가정)
chart = sheet._charts[0]
# 시리즈 와 카테고리 출력
print_chart_series_and_categories(chart)
# 엑셀 파일 닫기
wb.close()