简单的取值

本文介绍使用Python的openpyxl库读取Excel文件的方法,包括定位特定工作表及单元格,实现对资产负债表中年初存货数据的自动查找与提取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from openpyxl import load_workbook

'''
@File    :
@Author  :  william
@Time    :  2020/09/29
@notice  :  null
@coding  : utf-8
'''
# import xlrd
# from xlutils.copy import copy
import openpyxl

# 下一步还得训练程序
# 使其满足对excel文件名和excel工作薄的识别


ws_fuzhaibiao_nianchucunhuo = 0


nianchucunhuo_row = 0
nianchucunhuo_column = 0

#####################################################################锁定资产负债表和工作薄
# 如下代码用于多个相关表的打开操作
fn_fuzhaibiao = '资产负债表.xlsx'
wb_fuzhaibiao = openpyxl.load_workbook(fn_fuzhaibiao)

fuzhaibiao_allSheets = wb_fuzhaibiao.get_sheet_names()

for i in range(len(fuzhaibiao_allSheets)):
    fuzhaibiao_sheet = wb_fuzhaibiao.get_sheet_by_name(fuzhaibiao_allSheets[i])
    for row in fuzhaibiao_sheet.iter_rows():
        for cell in row:
            if str(str(cell.value).replace(' ', '')).find("资产负债表") != -1:
                fuzhaibiao_sheet_selected = wb_fuzhaibiao.get_sheet_by_name(fuzhaibiao_allSheets[i])

                break

ws_fuzhaibiao = wb_fuzhaibiao.get_sheet_by_name(fuzhaibiao_sheet_selected.title)
print(ws_fuzhaibiao)


########### 资产负债表     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 :
            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 = ws_fuzhaibiao.cell(row=nianchucunhuo_row, column=nianchucunhuo_column).value
        print(ws_fuzhaibiao_nianchucunhuo)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值