파이썬을 이용하여 10년치 재무제표 가져오기 - 3. 손익계산서, 현금흐름표 가져오기
본문 바로가기
파이썬으로 만든 것들/10년치 재무제표 가져오기

파이썬을 이용하여 10년치 재무제표 가져오기 - 3. 손익계산서, 현금흐름표 가져오기

by Squat Lee 2021. 5. 15.

 

지난 포스트에 이어서 손익계산서와 현금흐름까지 10년치 데이터를 가져와 보도록 하겠습니다.

2021.05.14 - [파이썬/Python Project] - 파이썬을 이용하여 10년치 재무제표 가져오기 - 2. 재무상태표 가져오기

 

파이썬을 이용하여 10년치 재무제표 가져오기 - 2. 재무상태표 가져오기

지난 포스트에서 DART(전자공시시스템)의 API를 통해 10년치 재무제표 데이터를 가지고 왔습니다. 가져온 데이터는 항목이 너무 많아서 분석이 어렵습니다. 분석을 할 수 있도록 필요한 항목만 빼

dotsnlines.tistory.com

 

손액계산서는 시트명이 'Data_is'로 되어 있네요. 변수로 저장합니다.

 

가져올 항목을 'is_items'라는 List에 저장합니다.

각 항목별 데이터를 위의 코드와 같이 가져옵니다.

 

첫번째 포스트에서 재무상태표를 가져오는 코드와 거의 같습니다. 엑셀파일의 행, 렬, 변수명 정도만 수정하였습니다.

 

현금흐름도 같은 방법으로 가져오면 됩니다.

 

제대로 결과가 출력 되는 것을 확인할 수 있습니다.

 

엑셀파일로 저장하니 훨씬 보기가 좋네요.

 

아래는 전체 코드이니 참고하시기 바랍니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from openpyxl import Workbook, load_workbook
import pandas as pd
 
# Company that you find
file = 'fs_samsung.xlsx'
 
wb = load_workbook(file)
 
# 재무상태표 시트 지정
ws_bs = wb['Data_bs']
 
# 년도 가져오기
y_list = []
= 10
while ws_bs.cell(2, c).value != None:
    y = ws_bs.cell(1, c).value
    if y[:1== '2':
        y_list.append(y)
    c += 1
 
# 가져올 항목 List로 만들기
bs_items = ['ifrs-full_Assets''ifrs-full_CurrentAssets''ifrs-full_CashAndCashEquivalents''dart_ShortTermTradeReceivable',
            'entity00126380_udf_BS_201710182279121_CurrentAssets''ifrs-full_Inventories''ifrs-full_Liabilities',
            'ifrs-full_CurrentLiabilities''ifrs-full_ShorttermBorrowings''ifrs-full_Equity''ifrs-full_IssuedCapital',
            'ifrs-full_RetainedEarnings']
 
# 재무상태표 Data 가져오기
df_list = []
for bs_item in bs_items:
    temp_list = []
    r = 4
    while ws_bs.cell(r,2).value != None:
        if ws_bs.cell(r,2).value == bs_item:
            item = ws_bs.cell(r, 3).value
            c = 10
            while ws_bs.cell(1,c).value != None:
                temp_list.append(ws_bs.cell(r,c).value)
                c += 1
        r += 1
    df = pd.DataFrame({item : temp_list}, index=y_list)
    df_list.append(df)
 
# 손익계산서 Data 가져오기
 
ws_is = wb['Data_is']
 
# 가져올 항목 List로 만들기
is_items = ['ifrs-full_Revenue''ifrs-full_CostOfSales''ifrs-full_GrossProfit',
            'dart_TotalSellingGeneralAdministrativeExpenses''dart_OperatingIncomeLoss',
            'ifrs-full_FinanceIncome''ifrs-full_ProfitLoss']
 
for is_item in is_items:
    temp_list = []
    r = 4
    while ws_is.cell(r,2).value != None:
        if ws_is.cell(r,2).value == is_item:
            item = ws_is.cell(r, 3).value
            c = 8
            while ws_is.cell(1,c).value != None:
                temp_list.append(ws_is.cell(r,c).value)
                c += 1
        r += 1
    df = pd.DataFrame({item : temp_list}, index=y_list)
    df_list.append(df)
 
 
# 현금흐름 Data 가져오기
 
ws_cf = wb['Data_cf']
 
# 가져올 항목 List로 만들기
cf_items = ['ifrs-full_CashFlowsFromUsedInOperatingActivities','ifrs-full_InterestPaidClassifiedAsOperatingActivities',
            'ifrs-full_CashFlowsFromUsedInInvestingActivities''ifrs-full_CashFlowsFromUsedInFinancingActivities',
            'dart_AcquisitionOfTreasuryShares',
            'entity00126380_udf_CF_20171021102249949_CashFlowsFromUsedInFinancingActivities',
            'ifrs-full_IncreaseDecreaseInCashAndCashEquivalents''dart_CashAndCashEquivalentsAtBeginningOfPeriodCf',
            'dart_CashAndCashEquivalentsAtEndOfPeriodCf']
 
for cf_item in cf_items:
    temp_list = []
    r = 4
    while ws_cf.cell(r,2).value != None:
        if ws_cf.cell(r,2).value == cf_item:
            item = ws_cf.cell(r, 3).value
            c = 9
            while ws_cf.cell(1,c).value != None:
                temp_list.append(ws_cf.cell(r,c).value)
                c += 1
        r += 1
    df = pd.DataFrame({item : temp_list}, index=y_list)
    df_list.append(df)
 
 
total_df = pd.concat(df_list, axis=1)
total_df = total_df.transpose()
 
print(total_df)
total_df.to_excel('재무상태표.xlsx')
 
cs

 

2021.05.16 - [분류 전체보기] - 파이썬을 이용하여 10년치 재무제표 가져오기 - 4. 코드 개선하기

 

파이썬을 이용하여 10년치 재무제표 가져오기 - 4. 코드 개선하기

지난번 포스트까지 DART(전자공시)에서 10년치 제무재표 데이터를 가져오는 파이썬 코드를 작성했습니다. 2021.05.15 - [파이썬/Python Project] - 파이썬을 이용하여 10년치 재무제표 가져오기 - 3. 손익계

dotsnlines.tistory.com

 

CAGR(연복리 수익률) 30%를 목표로 하는 파이썬 퀀트투자법이 궁금하시다면 아래 포스트를 클릭해 보시기 바랍니다.

2021.11.21 - [집구석 강의/파이썬 퀀트투자 쉽게하기] - 파이썬 퀀트투자 쉽게하기 - 1. 들어가는 글

 

파이썬 퀀트투자 쉽게하기 - 1. 들어가는 글

퀀트투자는 참으로 매력적인 투자방법입니다. 일정한 기준으로 종목을 선별해서 일정한 기간 후에 기계적으로 매도하고, 다시 같은 방법으로 매수합니다. 이런 작업을 계속해서 반복하다 보면

dotsnlines.tistory.com

 

728x90
반응형

댓글