분기 별 재무 보고서

import os 
import win32com.client as win32 
import shutil 
import openpyxl 
import docx 



##### 첫 번째 단계는 xlsx 파일을 원본 폴더에 놓고 bak 폴더에 직접 복사하는 것입니다. 슬라이스 rst 

str01_row = 0 
str01_column = 0 



mydir = ' 
    bak'if 
os.path.exists (mydir) : shutil.rmtree ( 'bak') 


os.mkdir (mydir) 



str01 = '' 


file = [] 



path = os.getcwd () # 프로그램이있는 파일 디렉토리 변환 전에 위치합니다. 

path_bak = os.getcwd () + r '/ bak /' 

pathdir = os.listdir (path) 
print (pathdir) 
for s in pathdir : 
    newdir = os.path.join (path, s) 
    if os. path.isfile (newdir) : 
        os.chdir (path_bak) 
        pathdir_bak = os.listdir (path_bak)
       # print (pathdir_bak) 
        if os.path.splitext (newdir) [1] == ".xls": 
            t = os.path.split (s) [1] + r'x ' 
            if os.path.exists (t ) : 
            # print (os.path.exists (os.path.split (s) [1])) 
                break 
            else : 
                excel = win32.gencache.EnsureDispatch ( 'Excel.Application') 
                wb = excel.Workbooks.Open (newdir ) 
             # print (newdir) 
                path_dir = os.path.split (newdir) [0] 
                path_file = os.path.split (newdir) [1] 
              # print (path_dir) 
             # print (path_file)
                wb.SaveAs (path_dir + '/ bak /'+ path_file + 'x', FileFormat = 51) # FileFormat = 51은 .xlsx 확장자 용입니다. 
                wb.Close () # FileFormat = 56은 .xls 확장자 용 
                excel.Application.Quit입니다. () 
        elif os.path.splitext (newdir) [1] == ".xlsx": 
            file.append (newdir) 


    elif os.path.isdir (newdir) : 
범위 내에서 i 
        계속 
(len (file)) : 
    pcm_file = 파일 [i] 
    경로 _0 = os.path.split (파일 [i]) [0] 
    경로 _1 = os.path.split (파일 [i]) [1] 
    이전 
    이름 = 경로 _0 + '/'+ 경로 _1 새 이름 = 경로 _0 + r '/ bak /'+ path_1 
    shutil.copyfile (oldname, newname) 




##### 두 번째 단계는 파일 디렉토리를 bak 폴더로 전환하고 uvw를 슬라이스하는 것입니다.


 
file_deal = [] 

path_deal = os.getcwd () # 변환 전에 프로그램이 위치한 파일 디렉토리 





pathdir_deal = os.listdir (path_deal) 
pathdir_bak에서 u를 : 
    newdir_deal = os.path.join (path_deal, u) 
    file_deal.append (newdir_deal) 
   # print (file_deal) 



범위 내 j의 경우 (len (file_deal)) : 
    pcm_file_deal = file_deal [j] 
    path_0_deal = os.path.split (file_deal [j]) [0] 
    # 파일이 위치한 디렉토리 path_1_deal = os.path.split (file_deal [j]) [1] # 접미사가있는 파일 이름 
  # print (pcm_file_deal) 
  # print (path_0_deal) 
    print (path_1_deal) 


    wb = openpyxl.load_workbook (path_1_deal) 

    allSheets = wb.get_sheet_names () 

    for i in range (len (allSheets) ) : 
        시트 = wb.get_sheet_by_name (allSheets [i]) 
        for row in sheet.iter_rows () : 
            for cell in row : 
                if str (str (cell.value) .replace ( '', '')). find ( "기업의 주요 경제 지표로 제한")! = -1 : 
                    sheet_selected = wb .get_sheet_by_name (allSheets [i]) 

                    print (sheet_selected) 
                    print (path_1_deal) 
                    fn = path_1_deal 
                    ws = wb.get_sheet_by_name (sheet_selected.title) 
                    break 




########### 대차 대조표 1 재고 시작 

### ## ###### 


ws.iter_rows ()의 행에 대한 연초의 대차 대조표 재고 : 
    행의 셀 : 
        if str (str (cell.value) .replace ( '', '')) .find ( "Total")! = -1 : 
            str01_row = cell.row 
            print (cell.row)
            print (str01_row) 
        else : : 
print (str01) 
wdoc = docx.Document () 
ptr = wdoc.add_paragraph (str)
            ws_str01 = 0

ws.iter_rows ()의 행 
            중단 : 
    행의 셀 : 
        if str (str (cell.value) .replace ( '', '')). find ( "上年 同期")! = -1 : 
            str01_column = cell.column 
            print (str01_column) 

            break 

if str01_row == 0 또는 str01_column == 0 : 
    str01 = '' 
else : 
    if ws.cell (row = str01_row, column = str01_column) .value! = None : 
        str01 = str (ws_fuzhaibiao. cell (row = nianchucunhuo_row, column = nianchucunhuo_column) .value) .replace ( ',', '') 


str = '济南 市 财务 季报 上报 单位'

wdoc.save ( 'Financial Quarterly Report.docx')

추천

출처blog.csdn.net/jidawanghao/article/details/112807712