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)
简单的取值
最新推荐文章于 2024-06-30 04:08:51 发布
本文介绍使用Python的openpyxl库读取Excel文件的方法,包括定位特定工作表及单元格,实现对资产负债表中年初存货数据的自动查找与提取。
3386

被折叠的 条评论
为什么被折叠?



