엑셀 자동화 중 차트를 그리기 위한 메소드입니다.
도움이 되실 분이 계실까 하여 올려봅니다.
import psutil
import win32com.client
for proc in psutil.process_iter():
if proc.name() == "EXCEL.EXE":
proc.kill()
excel = win32com.client.dynamic.Dispatch('Excel.Application')
fileName = r"C:\Users\hdec\Desktop\..."
wb = excel.Workbooks.Open(fileName)
excel.Visible = True
excel.DisplayAlerts = False
ws = wb.Sheets(1)
# sheetName = ws.Name
# usedRangeData = ws.UsedRange()
chart = wb.Charts.Add()
chart.HasTitle = True
chart.Name = '차트'
s1 = chart.SeriesCollection(1)
s2 = chart.SeriesCollection(2)
s3 = chart.SeriesCollection(3)
s4 = chart.SeriesCollection(4)
s5 = chart.SeriesCollection(5)
s6 = chart.SeriesCollection(6)
s7 = chart.SeriesCollection(7)
s8 = chart.SeriesCollection(8)
s9 = chart.SeriesCollection(9)
s10 = chart.SeriesCollection(10)
s11 = chart.SeriesCollection(11)
s2.Delete()
s4.Delete()
s5.Delete()
s6.Delete()
s7.Delete()
s9.Delete()
s10.Delete()
s1.Interior.ColorIndex = 5
s8.Interior.ColorIndex = 45
s3.ChartType = 4
s11.ChartType = 4
s3.Border.ColorIndex = 10
s11.Border.ColorIndex = 3
s1.AxisGroup = 1
s8.AxisGroup = 1
s3.AxisGroup = 2
s11.AxisGroup = 2
chart.HasTitle = True
chart.ChartTitle.Text = "■ ㄴㄴㄴ "
chart.ChartTitle.Left = 10
x_axis = chart.Axes(1)
y1_axis = chart.Axes(2, 1)
y2_axis = chart.Axes(2, 2)
# x_axis.HasTitle = True
y1_axis.HasTitle = True
y2_axis.HasTitle = True
# x_axis.AxisTitle.Text = "년월"
y1_axis.AxisTitle.Text = "ㄴㄴㄴ"
y2_axis.AxisTitle.Text = "ㄴㄴㄴㄴ (%)"
y1_axis.TickLabels.NumberFormat = '#,,'
y2_axis.TickLabels.NumberFormat = '0'
y1_axis.MinimumScale = 0
chart.ChartArea.Interior.ColorIndex = 0
chart.PlotArea.Interior.ColorIndex = 34
chart.ChartArea.Border.ColorIndex = 2
chart.PlotArea.Border.ColorIndex = 2
y1_axis.MajorGridLines.Delete()
wb.Save()
wb.Close(False)
'코딩' 카테고리의 다른 글
Ajax, django 협업 코드 (0) | 2022.08.02 |
---|---|
Pandas, 데이터 컨트롤링 예시 코드 (0) | 2022.08.02 |
데이터프레임, 파이썬 공부하기 (0) | 2021.10.10 |
보안프로그램 하에서 엑셀 읽어와서 판다스 데이터프레임으로 (0) | 2021.08.09 |
집 뒤의 산책길 (0) | 2021.06.23 |