一气呵成:一键式统计4.0批量转换xls,加动态数据提取

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

import time

#####第一步先降原文件夹中的xlsx文件直接拷贝至bak文件夹中,切片r s t

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


os.mkdir(mydir)

#time.sleep(2)  #暂停10秒


ws_fuzhaibiao_nianchucunhuo = 0
ws_fuzhaibiao_liudongzichanheji = 0
ws_fuzhaibiao_yingshouzhangkuan = 0
ws_fuzhaibiao_nianchucunhuo = 0
ws_fuzhaibiao_qimocunhuo = 0
ws_fuzhaibiao_gudingzichanyuanjia = 0
ws_fuzhaibiao_leijizhejiu = 0
ws_fuzhaibiao_bennianzhejiu = 0
ws_fuzhaibiao_zichanzongji = 0
ws_fuzhaibiao_fuzhaiheji = 0

ws_lirunbiao_yingyeshouru = 0
ws_lirunbiao_yingyechengben = 0
ws_lirunbiao_shuijinjifujia = 0
ws_lirunbiao_xiaoshoufeiyong = 0
ws_lirunbiao_guanlifeiyong = 0
ws_lirunbiao_yanfafeiyong = 0
ws_lirunbiao_caiwufeiyong = 0
ws_lirunbiao_lixishouru = 0
ws_lirunbiao_lixifeiyong = 0
ws_lirunbiao_guanlifeiyong = 0
ws_lirunbiao_yanfafeiyong = 0
ws_lirunbiao_caiwufeiyong = 0
ws_lirunbiao_lixishouru = 0
ws_lirunbiao_lixifeiyong = 0
ws_lirunbiao_yingyelirun = 0
ws_lirunbiao_yingyewaishouru = 0
ws_lirunbiao_yingyewaizhichu = 0
ws_lirunbiao_lirunzonge = 0
ws_lirunbiao_suodeshuifeiyong = 0

ws_nashuishenbaobiao_xiaoxiangshuie = 0
ws_nashuishenbaobiao_jinxiangshuie = 0
ws_nashuishenbaobiao_jinxiangshuiezhuanchu = 0
ws_nashuishenbaobiao_yingtuishuie = 0
ws_nashuishenbaobiao_yingnashuie = 0
ws_nashuishenbaobiao_yingbujiaoshuie = 0
ws_nashuishenbaobiao_yingnashuiejianzhenge = 0

nianchucunhuo_row = 0
nianchucunhuo_column = 0

liudongzichanheji_row = 0
liudongzichanheji_column = 0

yingshouzhangkuan_row = 0
yingshouzhangkuan_column = 0

qimocunhuo_row = 0
qimocunhuo_column = 0

gudingzichanyuanjia_row = 0
gudingzichanyuanjia_column = 0

leijizhejiu_row = 0
leijizhejiu_column = 0

bennianzhejiu_row = 0
bennianzhejiu_column = 0

zichanzongji_row = 0
zichanzongji_column = 0

fuzhaiheji_row = 0
fuzhaiheji_column = 0
fuzhaiheji_row_column = 0

yingyeshouru_row = 0
yingyeshouru_column = 0

yingyechengben_row = 0
yingyechengben_column = 0

shuijinjifujia_row = 0
shuijinjifujia_column = 0

xiaoshoufeiyong_row = 0
xiaoshoufeiyong_column = 0

guanlifeiyong_row = 0
guanlifeiyong_column = 0

yanfafeiyong_row = 0
yanfafeiyong_column = 0

caiwufeiyong_row = 0
caiwufeiyong_column = 0

lixishouru_row = 0
lixishouru_column = 0

lixifeiyong_row = 0
lixifeiyong_column = 0

yingyelirun_row = 0
yingyelirun_column = 0

yingyewaishouru_row = 0
yingyewaishouru_column = 0

yingyewaizhichu_row = 0
yingyewaizhichu_column = 0

lirunzonge_row = 0
lirunzonge_column = 0

suodeshuifeiyong_row = 0
suodeshuifeiyong_column = 0

yingjiaozengzhishui_row = 0
yingjiaozengzhishui_column = 0

pingjunrenshu_row = 0
pingjunrenshu_column = 0

xiaoxiangshuie_row = 0
xiaoxiangshuie_column = 0

jinxiangshuie_row = 0
jinxiangshuie_column = 0

jinxiangshuiezhuanchu_row = 0
jinxiangshuiezhuanchu_column = 0

yingtuishuie_row = 0
yingtuishuie_column = 0

yingnashuie_row = 0
yingnashuie_column = 0

yingbujiaoshuie_row = 0
yingbujiaoshuie_column = 0

yingnashuiejianzhenge_row = 0
yingnashuiejianzhenge_column = 0

fuzhaiheji_row = 0
fuzhaiheji_column = 0


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 is for .xlsx extension
                wb.Close()  # FileFormat = 56 is for .xls extension
                excel.Application.Quit()
        elif os.path.splitext(newdir)[1] == ".xlsx":
            file.append(newdir)


    elif os.path.isdir(newdir):
        continue

for i in range(len(file)):
    pcm_file = file[i]
    path_0 = os.path.split(file[i])[0]
    path_1 = os.path.split(file[i])[1]
    oldname = path_0 + '/' + path_1
    newname = path_0 + r'/bak/' + path_1
    shutil.copyfile(oldname, newname)


#time.sleep(5)  #暂停10秒

#####第二步将文件目录切换至bak文件夹中,,切片u v w



file_deal = []

path_deal = os.getcwd()  # 转换前程序所在文件目录





pathdir_deal = os.listdir(path_deal)
for u in pathdir_bak:
    newdir_deal = os.path.join(path_deal, u)
    file_deal.append(newdir_deal)
   # print(file_deal)



for j in range(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)):
        sheet = 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_fuzhaibiao = path_1_deal
                    ws_fuzhaibiao = wb.get_sheet_by_name(sheet_selected.title)
                    continue
                elif str(str(cell.value).replace(' ', '')).find("利润表") != -1 or str(str(cell.value).replace(' ', '')).find("损益表") != -1:
                    sheet_selected = wb.get_sheet_by_name(allSheets[i])

                    print(sheet_selected)
                    print(path_1_deal)
                    fn_lirunbiao = path_1_deal
                    ws_lirunbiao = wb.get_sheet_by_name(sheet_selected.title)
                    continue
                elif str(str(cell.value).replace(' ', '')).find("一般纳税人适用") != -1 or str(str(cell.value).replace(' ', '')).find("小规模纳税人适用") != -1:
                    sheet_selected = wb.get_sheet_by_name(allSheets[i])

                    print(sheet_selected)
                    print(path_1_deal)
                    fn_nashuishenbaobiao = path_1_deal
                    ws_nashuishenbaobiao = wb.get_sheet_by_name(sheet_selected.title)
                    continue

##############此处让这个循环体循环三遍,目的是避免一个excel表中有三个表


##############################################################################锁定财务表和工作薄

fn_caiwuzhuangkuangbiao = '财务状况表.xlsx'
wb_caiwuzhuangkuangbiao = openpyxl.load_workbook(fn_caiwuzhuangkuangbiao)

ws_caiwuzhuangkuangbiao = wb_caiwuzhuangkuangbiao.get_sheet_by_name("财务状况表")

################################################锁定纳税申报表和工作薄


# 如下代码用于将多个表中的相关数据写入财务状况表


########### 资产负债表     1年初存货


for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("存货") != -1:
            nianchucunhuo_row = cell.row
        else:
            ws_fuzhaibiao_nianchucunhuo = 0

            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("年初余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "年初数") != -1:
            nianchucunhuo_column = cell.column

            break

if nianchucunhuo_row == 0 or nianchucunhuo_column == 0:
    ws_fuzhaibiao_nianchucunhuo = 0
else:
    if ws_fuzhaibiao.cell(row=nianchucunhuo_row, column=nianchucunhuo_column).value != None:
        ws_fuzhaibiao_nianchucunhuo = round(float(
            str(ws_fuzhaibiao.cell(row=nianchucunhuo_row, column=nianchucunhuo_column).value).replace(',', '')) / 1000)

########### 资产负债表    2流动资产合计

for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        # print(cell.value)
        if str(str(cell.value).replace(' ', '')).find("流动资产合计") != -1 and str(cell.value).find("非流动资产合计") == -1:
            liudongzichanheji_row = cell.row
            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            liudongzichanheji_column = cell.column
            #  print(zichanheji_column)
            break

if liudongzichanheji_row == 0 or liudongzichanheji_column == 0:
    ws_fuzhaibiao_liudongzichanheji = 0
else:

    if ws_fuzhaibiao.cell(row=liudongzichanheji_row, column=liudongzichanheji_column).value != None:
        ws_fuzhaibiao_liudongzichanheji = round(float(
            str(ws_fuzhaibiao.cell(row=liudongzichanheji_row, column=liudongzichanheji_column).value).replace(',',
                                                                                                              '')) / 1000)

########################################资产负债表  应收账款


for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("应收账款") != -1:
            yingshouzhangkuan_row = cell.row
            # print(zichanyuanjia_row)
            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            yingshouzhangkuan_column = cell.column
            #  print(zichanyuanjia_column)
            break

if yingshouzhangkuan_row == 0 or yingshouzhangkuan_column == 0:
    ws_fuzhaibiao_yingshouzhangkuan = 0
else:

    if ws_fuzhaibiao.cell(row=yingshouzhangkuan_row, column=yingshouzhangkuan_column).value != None:
        ws_fuzhaibiao_yingshouzhangkuan = round(float(str(
            ws_fuzhaibiao.cell(row=yingshouzhangkuan_row, column=yingshouzhangkuan_column).value).replace(',',
                                                                                                          '')) / 1000)

################################      资产负债表    4存货

for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("存货") != -1:
            qimocunhuo_row = cell.row
            #    print(cunhuo_row)

            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            qimocunhuo_column = cell.column
            #   print(cunhuo_column)
            break

if qimocunhuo_row == 0 or qimocunhuo_column == 0:
    ws_fuzhaibiao_qimocunhuo = 0
else:

    if ws_fuzhaibiao.cell(row=qimocunhuo_row, column=qimocunhuo_column).value != None:
        ws_fuzhaibiao_qimocunhuo = round(
            float(str(ws_fuzhaibiao.cell(row=qimocunhuo_row, column=qimocunhuo_column).value).replace(',', '')) / 1000)

########### 资产负债表    5固定资产原价


for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("固定资产原价") != -1:
            gudingzichanyuanjia_row = cell.row
            # print(zichanyuanjia_row)
            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            gudingzichanyuanjia_column = cell.column
            #  print(zichanyuanjia_column)
            break

if gudingzichanyuanjia_row == 0 or gudingzichanyuanjia_column == 0:
    ws_fuzhaibiao_gudingzichanyuanjia = 0
else:
    if ws_fuzhaibiao.cell(row=gudingzichanyuanjia_row, column=gudingzichanyuanjia_column).value != None:
        ws_fuzhaibiao_gudingzichanyuanjia = round(float(str(
            ws_fuzhaibiao.cell(row=gudingzichanyuanjia_row, column=gudingzichanyuanjia_column).value).replace(',',
                                                                                                              '')) / 1000)

########### 资产负债表    6累计折旧

for row in ws_fuzhaibiao.iter_rows():

    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("累计折旧") != -1:
            leijizhejiu_row = cell.row

            print(leijizhejiu_row)
            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            leijizhejiu_column = cell.column
            print(leijizhejiu_column)
            break

if leijizhejiu_row == 0 or leijizhejiu_column == 0:
    ws_fuzhaibiao_leijizhejiu = 0
    print(ws_fuzhaibiao_leijizhejiu)
else:
    if ws_fuzhaibiao.cell(row=leijizhejiu_row, column=leijizhejiu_column).value != None:
        ws_fuzhaibiao_leijizhejiu = round(float(str(
            ws_fuzhaibiao.cell(row=leijizhejiu_row, column=leijizhejiu_column).value).replace(',', '')) / 1000)

#######################7本年折旧

########### 资产负债表   8资产总计

for row in ws_fuzhaibiao.iter_rows():
    for cell in row:

        if str(str(cell.value).replace(' ', '')).find("资产总计") != -1:
            zichanzongji_row = cell.row
            #      print(zichanzongji_row)
            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            zichanzongji_column = cell.column
            # print(zichanzongji_column)
            break

if zichanzongji_row == 0 or zichanzongji_column == 0:
    ws_fuzhaibiao_zichanzongji = 0
else:
    if ws_fuzhaibiao.cell(row=zichanzongji_row, column=zichanzongji_column).value != None:
        ws_fuzhaibiao_zichanzongji = round(float(str(
            ws_fuzhaibiao.cell(row=zichanzongji_row, column=zichanzongji_column).value).replace(',', '')) / 1000)

########### 资产负债表   9负债合计


for row in ws_fuzhaibiao.iter_rows():
    for cell in row:

        if str(str(cell.value).replace(' ', '')).find("负债合计") != -1 and str(cell.value).find("流动负债合计") == -1 and str(
                cell.value).find("非流动负债合计") == -1:
            fuzhaiheji_row = cell.row
            fuzhaiheji_row_column = cell.column

            break
for row in ws_fuzhaibiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("期末余额") != -1 or str(str(cell.value).replace(' ', '')).find(
                "期末数") != -1:
            if cell.column > fuzhaiheji_row_column:
                fuzhaiheji_column = cell.column

                #   print(fuzhaiheji_column)
                break
if fuzhaiheji_row == 0 or fuzhaiheji_column == 0:
    ws_fuzhaibiao_hezhaiheji = 0
else:
    if ws_fuzhaibiao.cell(row=fuzhaiheji_row, column=fuzhaiheji_column).value != None:
        ws_fuzhaibiao_fuzhaiheji = round(
            float(str(ws_fuzhaibiao.cell(row=fuzhaiheji_row, column=fuzhaiheji_column).value).replace(',', '')) / 1000)

########### 利润表   10营业收入


for row in ws_lirunbiao.iter_rows():

    for cell in row:
        #   print(cell.value)
        if str(str(cell.value).replace(' ', '')).find("营业收入") != -1:
            yingyeshouru_row = cell.row
            #     print(yingyeshouru_row)
            break
for row in ws_lirunbiao.iter_rows():
    #  print(cell.value)
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            yingyeshouru_column = cell.column
            #    print(yingyeshouru_column)
            break

if yingyeshouru_row == 0 or yingyeshouru_column == 0:
    ws_lirunbiao_yingyeshouru = 0
else:
    if ws_lirunbiao.cell(row=yingyeshouru_row, column=yingyeshouru_column).value != None:
        ws_lirunbiao_yingyeshouru = round(float(str(
            ws_lirunbiao.cell(row=yingyeshouru_row, column=yingyeshouru_column).value).replace(',', '')) / 1000)

########### 利润表    11营业成本

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("营业成本") != -1:
            yingyechengben_row = cell.row
            #  print(yingyechengben_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            yingyechengben_column = cell.column
            #     print(yingyechengben_column)
            break

if yingyechengben_row == 0 or yingyechengben_column == 0:
    ws_lirunbiao_yingyechengben = 0
else:
    if ws_lirunbiao.cell(row=yingyechengben_row, column=yingyechengben_column).value != None:
        ws_lirunbiao_yingyechengben = round(float(str(
            ws_lirunbiao.cell(row=yingyechengben_row, column=yingyechengben_column).value).replace(',', '')) / 1000)

########### 利润表    12税金及附加


for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("税金及附加") != -1 or str(str(cell.value).replace(' ', '')).find(
                "主营业务税金及附加") == -1:
            shuijinjifujia_row = cell.row
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            shuijinjifujia_column = cell.column
            #   print(fujia_column)
            break
if shuijinjifujia_row == 0 or shuijinjifujia_column == 0:
    ws_lirunbiao_shuijinjifujia = 0
else:
    if ws_lirunbiao.cell(row=shuijinjifujia_row, column=shuijinjifujia_column).value != None:
        ws_lirunbiao_shuijinjifujia = round(float(str(
            ws_lirunbiao.cell(row=shuijinjifujia_row, column=shuijinjifujia_column).value).replace(',', '')) / 1000)

########### 利润表    13销售费用


for row in ws_lirunbiao.iter_rows():
    for cell in row:

        if str(str(cell.value).replace(' ', '')).find("销售费用") != -1:
            xiaoshoufeiyong_row = cell.row

            #  print(xiaoshoufeiyong_row)
            break

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            xiaoshoufeiyong_column = cell.column

            #  print(xiaoshoufeiyong_column)
            break

if xiaoshoufeiyong_row == 0 or xiaoshoufeiyong_column == 0:
    ws_lirunbiao_xiaoshoufeiyong = 0
else:
    if ws_lirunbiao.cell(row=xiaoshoufeiyong_row, column=xiaoshoufeiyong_column).value != None:
        ws_lirunbiao_xiaoshoufeiyong = round(float(str(
            ws_lirunbiao.cell(row=xiaoshoufeiyong_row, column=xiaoshoufeiyong_column).value).replace(',', '')) / 1000)

########### 利润表   14管理费用

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("管理费用") != -1:
            guanlifeiyong_row = cell.row
            #   print(guanlifeiyong_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            guanlifeiyong_column = cell.column
            #   print(guanlifeiyong_column)
            break

if guanlifeiyong_row == 0 or guanlifeiyong_column == 0:
    ws_lirunbiao_guanlifeiyong = 0
else:
    if ws_lirunbiao.cell(row=guanlifeiyong_row, column=guanlifeiyong_column).value != None:
        ws_lirunbiao_guanlifeiyong = round(float(str(
            ws_lirunbiao.cell(row=guanlifeiyong_row, column=guanlifeiyong_column).value).replace(',', '')) / 1000)

########### 利润表  15研发费用 研究费用?

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("研究费用") != -1 or str(str(cell.value).replace(' ', '')).find(
                "研发费用") != -1:
            yanfafeiyong_row = cell.row
            #   print(yanfafeiyong_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            yanfafeiyong_column = cell.column
            #  print(yanfafeiyong_column)
            break

if yanfafeiyong_row == 0 or yanfafeiyong_column == 0:
    ws_lirunbiao_yanfafeiyong = 0
else:

    if ws_lirunbiao.cell(row=yanfafeiyong_row, column=yanfafeiyong_column).value != None:
        ws_lirunbiao_yanfa = round(float(
            str(ws_lirunbiao.cell(row=yanfafeiyong_row, column=yanfafeiyong_column).value).replace(',', '')) / 1000)

########### 利润表    16财务费用

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("财务费用") != -1:
            caiwufeiyong_row = cell.row
            #  print(caiwufeiyong_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            caiwufeiyong_column = cell.column
            #   print(caiwufeiyong_column)
            break

if caiwufeiyong_row == 0 or caiwufeiyong_column == 0:
    ws_lirunbiao_caiwufeiyong = 0
else:
    if ws_lirunbiao.cell(row=caiwufeiyong_row, column=caiwufeiyong_column).value != None:
        ws_lirunbiao_caiwufeiyong = round(float(str(
            ws_lirunbiao.cell(row=caiwufeiyong_row, column=caiwufeiyong_column).value).replace(',', '')) / 1000)

######################################################### 利润表 17利息收入


for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("利息费用") != -1:
            lixishouru_row = cell.row
            #  print(caiwufeiyong_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            lixishouru_column = cell.column
            #   print(caiwufeiyong_column)
            break
if lixishouru_row == 0 or lixishouru_column == 0:
    ws_lirunbiao_lixishouru = 0
else:

    if ws_lirunbiao.cell(row=lixishouru_row, column=lixishouru_column).value != None:
        if ws_lirunbiao.cell(row=lixishouru_row, column=lixishouru_column).value < 0:
            ws_lirunbiao_lixishouru = round(float(str(
                ws_lirunbiao.cell(row=lixishouru_row, column=lixishouru_column).value).replace(',', '')) / 1000)
#################################################         利润表  18利息费用
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("利息费用") != -1:
            lixifeiyong_row = cell.row
            #  print(caiwufeiyong_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            lixifeiyong_column = cell.column
            #   print(caiwufeiyong_column)
            break
if lixifeiyong_row == 0 or lixifeiyong_column == 0:
    ws_lirunbiao_lixifeiyong = 0
else:
    if ws_lirunbiao.cell(row=lixifeiyong_row, column=lixifeiyong_column).value != None:
        if ws_lirunbiao.cell(row=lixifeiyong_row, column=lixifeiyong_column).value > 0:
            ws_lirunbiao_lixifeiyong = round(float(str(
                ws_lirunbiao.cell(row=lixifeiyong_row, column=lixifeiyong_column).value).replace(',', '')) / 1000)

########### 利润表   19营业利润

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("营业利润") != -1:
            yingyelirun_row = cell.row
            #  print(yingyelirun_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            yingyelirun_column = cell.column
            # print(yingyelirun_column)
            break

if yingyelirun_row == 0 or yingyelirun_column == 0:
    ws_lirunbiao_yingyelirun = 0
else:
    if ws_lirunbiao.cell(row=yingyelirun_row, column=yingyelirun_column).value != None:
        ws_lirunbiao_yingyelirun = round(
            float(str(ws_lirunbiao.cell(row=yingyelirun_row, column=yingyelirun_column).value).replace(',', '')) / 1000)

########### 利润表   20营业外收入


for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("营业外收入") != -1:
            yingyewaishouru_row = cell.row
            # print(yingyewaishouru_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            yingyewaishouru_column = cell.column
            #  print(yingyewaishouru_column)
            break

if yingyewaishouru_row == 0 or yingyewaishouru_column == 0:
    ws_lirunbiao_yingyewaishouru = 0
else:
    if ws_lirunbiao.cell(row=yingyewaishouru_row, column=yingyewaishouru_column).value != None:
        ws_lirunbiao_yingyewaishouru = round(float(str(
            ws_lirunbiao.cell(row=yingyewaishouru_row, column=yingyewaishouru_column).value).replace(',', '')) / 1000)

########### 利润表   营业外支出

for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("营业外支出") != -1:
            yingyewaizhichu_row = cell.row
            # print(yingyewaizhichu_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            yingyewaizhichu_column = cell.column
            # print(yingyewaizhichu_column)
            break

if yingyewaizhichu_row == 0 or yingyewaizhichu_column == 0:
    ws_lirunbiao_yingyewaizhichu = 0
else:
    if ws_lirunbiao.cell(row=yingyewaizhichu_row, column=yingyewaizhichu_column).value != None:
        # print(type( ws_lirunbiao.cell(row=yingyewaizhichu_row, column=yingyewaizhichu_column).value))
        ws_lirunbiao_yingyewaizhichu = round(float(str(
            ws_lirunbiao.cell(row=yingyewaizhichu_row, column=yingyewaizhichu_column).value).replace(',', '')) / 1000)

########### 利润表   22利润总额


for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("利润总额") != -1:
            lirunzonge_row = cell.row
            print("**********")
            print(lirunzonge_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            lirunzonge_column = cell.column
            print(lirunzonge_column)
            break

if lirunzonge_row == 0 or lirunzonge_column == 0:
    ws_lirunbiao_lirunzonge = 0
else:
    if ws_lirunbiao.cell(row=lirunzonge_row, column=lirunzonge_column).value != None:
        ws_lirunbiao_lirunzonge = round(
            float(str(ws_lirunbiao.cell(row=lirunzonge_row, column=lirunzonge_column).value).replace(',', '')) / 1000)

########### 利润表   23所得税费用


for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("所得税费用") != -1:
            suodeshuifeiyong_row = cell.row
            #  print(suodeshuifeiyong_row)
            break
for row in ws_lirunbiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计数") != -1 or str(str(cell.value).replace(' ', '')).find(
                "本年累计金额") != -1:
            suodeshuifeiyong_column = cell.column
            #  print(suodeshuifeiyong_column)
            break
if suodeshuifeiyong_row == 0 or suodeshuifeiyong_column == 0:
    ws_lirunbiao_suodeshuifeiyong = 0
else:
    if ws_lirunbiao.cell(row=suodeshuifeiyong_row, column=suodeshuifeiyong_column).value != None:
        ws_lirunbiao_suodeshuifeiyong = round(float(str(
            ws_lirunbiao.cell(row=suodeshuifeiyong_row, column=suodeshuifeiyong_column).value).replace(',', '')) / 1000)

########### 纳税申报表    应交增值税


##############纳税申报表    应交增值税  24.1 销项税额


for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("销项税额") != -1:
            xiaoxiangshuie_row = cell.row
            print(xiaoxiangshuie_row)
            #  print(xiaoxiangshuie_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            xiaoxiangshuie_column = cell.column
            #  print(xiaoxiangshuie_column)
            break

if xiaoxiangshuie_row == 0 or xiaoxiangshuie_column == 0:
    ws_nashuishenbaobiao_xiaoxiangshuie = 0
else:
    if ws_nashuishenbaobiao.cell(row=xiaoxiangshuie_row, column=xiaoxiangshuie_column).value != None:
        ws_nashuishenbaobiao_xiaoxiangshuie = round(float(str(
            ws_nashuishenbaobiao.cell(row=xiaoxiangshuie_row, column=xiaoxiangshuie_column).value).replace(',',
                                                                                                           '')) / 1000)

##############纳税申报表    应交增值税   24.2进项税额


for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("进项税额") != -1 and str(str(cell.value).replace(' ', '')).find(
                "进项税额转出") == -1:
            jinxiangshuie_row = cell.row
            #   print(jinxiangshuie_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            jinxiangshuie_column = cell.column
            #  print(jinxiangshuie_column)
            break
if jinxiangshuie_row == 0 or jinxiangshuie_column == 0:
    ws_nashuishenbaobiao_jinxiangshuie = 0
else:
    if ws_nashuishenbaobiao.cell(row=jinxiangshuie_row, column=jinxiangshuie_column).value != None:
        ws_nashuishenbaobiao_jinxiangshuie = round(float(str(
            ws_nashuishenbaobiao.cell(row=jinxiangshuie_row, column=jinxiangshuie_column).value).replace(',',
                                                                                                         '')) / 1000)

##############纳税申报表    应交增值税   24.3进项税额转出

for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("进项税额转出") != -1:
            jinxiangshuiezhuanchu_row = cell.row
            #    print(jinxiangshuiezhuanchu_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            jinxiangshuiezhuanchu_column = cell.column
            #   print(jinxiangshuiezhuanchu_column)
            break
if jinxiangshuiezhuanchu_row == 0 or jinxiangshuiezhuanchu_column == 0:
    ws_nashuishenbaobiao_jinxiangshuiezhuanchu = 0
else:
    if ws_nashuishenbaobiao.cell(row=jinxiangshuiezhuanchu_row, column=jinxiangshuiezhuanchu_column).value != None:
        ws_nashuishenbaobiao_jinxiangshuiezhuanchu = round(float(str(
            ws_nashuishenbaobiao.cell(row=jinxiangshuiezhuanchu_row,
                                      column=jinxiangshuiezhuanchu_column).value).replace(',', '')) / 1000)

##############纳税申报表    应交增值税   24.4免、抵、退应退税额

for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("免、抵、退应退税额") != -1:
            yingtuishuie_row = cell.row
            #  print(yingtuishuie_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            yingtuishuie_column = cell.column
            # print(yingtuishuie_column)
            break

if yingtuishuie_row == 0 or yingtuishuie_column == 0:
    ws_nashuishenbaobiao_yingtuishuie = 0
else:
    if ws_nashuishenbaobiao.cell(row=yingtuishuie_row, column=yingtuishuie_column).value != None:
        ws_nashuishenbaobiao_yingtuishuie = round(float(str(
            ws_nashuishenbaobiao.cell(row=yingtuishuie_row, column=yingtuishuie_column).value).replace(',', '')) / 1000)

##############纳税申报表    应交增值税   24.5简易计税办法计算的应纳税额


for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("简易计税办法计算的应纳税额") != -1:
            yingnashuie_row = cell.row
            #  print(yingnashuie_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            yingnashuie_column = cell.column
            #  print(yingnashuie_column)
            break

if yingnashuie_row == 0 or yingnashuie_column == 0:
    ws_nashuishenbaobiao_yingnashuie = 0
else:
    if ws_nashuishenbaobiao.cell(row=yingnashuie_row, column=yingnashuie_column).value != None:
        ws_nashuishenbaobiao_yingnashuie = round(float(str(
            ws_nashuishenbaobiao.cell(row=yingnashuie_row, column=yingnashuie_column).value).replace(',', '')) / 1000)

##############纳税申报表    应交增值税   24.6按简易计税办法计算的纳税检查应补缴税额


for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("按简易计税办法计算的纳税检查应补缴税额") != -1:
            yingbujiaoshuie_row = cell.row
            #  print(yingbujiaoshuie_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            yingbujiaoshuie_column = cell.column
            #  print(yingbujiaoshuie_column)
            break
if yingbujiaoshuie_row == 0 or yingbujiaoshuie_column == 0:
    ws_nashuishenbaobiao_yingbujiaoshuie = 0
else:
    if ws_nashuishenbaobiao.cell(row=yingbujiaoshuie_row, column=yingbujiaoshuie_column).value != None:
        ws_nashuishenbaobiao_yingbujiaoshuie = round(float(str(
            ws_nashuishenbaobiao.cell(row=yingbujiaoshuie_row, column=yingbujiaoshuie_column).value).replace(',',
                                                                                                             '')) / 1000)
# print(ws_nashuishenbaobiao_Z31)


##############纳税申报表    应交增值税   24.7应纳税额减征额

for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("应纳税额减征额") != -1:
            yingnashuiejianzhenge_row = cell.row
            #  print(yingnashuiejianzhenge_row)
            break
for row in ws_nashuishenbaobiao.iter_rows():
    for cell in row:
        if str(str(cell.value).replace(' ', '')).find("本年累计") != -1 or str(str(cell.value).replace(' ', '')).find(
                "累计金额") != -1:
            yingnashuiejianzhenge_column = cell.column
            #  print(yingnashuiejianzhenge_column)
            break
if yingnashuiejianzhenge_row == 0 or yingnashuiejianzhenge_column == 0:
    ws_nashuishenbaobiao_yingnashuiejianzhenge = 0
else:
    if ws_nashuishenbaobiao.cell(row=yingnashuiejianzhenge_row, column=yingnashuiejianzhenge_column).value != None:
        ws_nashuishenbaobiao_yingnashuiejianzhenge = round(float(str(
            ws_nashuishenbaobiao.cell(row=yingnashuiejianzhenge_row,
                                      column=yingnashuiejianzhenge_column).value).replace(',', '')) / 1000)
# print(ws_nashuishenbaobiao_Z32)


ws_caiwuzhuangkuangbiao['E3'] = ws_fuzhaibiao_nianchucunhuo
ws_caiwuzhuangkuangbiao['E5'] = ws_fuzhaibiao_liudongzichanheji
ws_caiwuzhuangkuangbiao['E6'] = ws_fuzhaibiao_yingshouzhangkuan
ws_caiwuzhuangkuangbiao['E7'] = ws_fuzhaibiao_qimocunhuo
ws_caiwuzhuangkuangbiao['E8'] = ws_fuzhaibiao_gudingzichanyuanjia
ws_caiwuzhuangkuangbiao['E9'] = ws_fuzhaibiao_leijizhejiu
ws_caiwuzhuangkuangbiao['E10'] = ws_fuzhaibiao_bennianzhejiu
ws_caiwuzhuangkuangbiao['E11'] = ws_fuzhaibiao_zichanzongji
ws_caiwuzhuangkuangbiao['E12'] = ws_fuzhaibiao_fuzhaiheji
ws_caiwuzhuangkuangbiao['E14'] = ws_lirunbiao_yingyeshouru
ws_caiwuzhuangkuangbiao['E15'] = ws_lirunbiao_yingyechengben
ws_caiwuzhuangkuangbiao['E16'] = ws_lirunbiao_shuijinjifujia
ws_caiwuzhuangkuangbiao['E17'] = ws_lirunbiao_xiaoshoufeiyong
ws_caiwuzhuangkuangbiao['E18'] = ws_lirunbiao_guanlifeiyong
ws_caiwuzhuangkuangbiao['E19'] = ws_lirunbiao_yanfafeiyong
ws_caiwuzhuangkuangbiao['E20'] = ws_lirunbiao_caiwufeiyong
ws_caiwuzhuangkuangbiao['E21'] = ws_lirunbiao_lixishouru
ws_caiwuzhuangkuangbiao['E22'] = ws_lirunbiao_lixifeiyong
ws_caiwuzhuangkuangbiao['E23'] = ws_lirunbiao_yingyelirun
ws_caiwuzhuangkuangbiao['E24'] = ws_lirunbiao_yingyewaishouru
ws_caiwuzhuangkuangbiao['E25'] = ws_lirunbiao_yingyewaizhichu
ws_caiwuzhuangkuangbiao['E26'] = ws_lirunbiao_lirunzonge
ws_caiwuzhuangkuangbiao['E27'] = ws_lirunbiao_suodeshuifeiyong

# 如下代码用于将多个表中的相关数据写入财务状况表------特例:利润表中利息费用指标,如果为正计财务状况表的利息收入,如果为负数计财务状况表的利息费用

# print(ws_lirunbiao_C22)


ws_caiwuzhuangkuangbiao['E28'] = ws_nashuishenbaobiao_xiaoxiangshuie - (
        ws_nashuishenbaobiao_jinxiangshuie - ws_nashuishenbaobiao_jinxiangshuiezhuanchu - ws_nashuishenbaobiao_yingtuishuie) + ws_nashuishenbaobiao_yingnashuie + ws_nashuishenbaobiao_yingbujiaoshuie - ws_nashuishenbaobiao_yingnashuiejianzhenge

# 保存财务状况表中修改的数据


wb_caiwuzhuangkuangbiao.save('财务状况表.xlsx')



shutil.copy(path_bak + r'/财务状况表.xlsx',path + r'/财务状况表.xlsx')



猜你喜欢

转载自blog.csdn.net/jidawanghao/article/details/112764405