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_qiyeshu = 0
qiyeshu_row = 0
qiyeshu_column = 0
ws_zichanzongji = 0
zichanzongji_row = 0
zichanzongji_column = 0
#####################################################################锁定资产负债表和工作薄
# 如下代码用于多个相关表的打开操作
fn = '资产负债表.xlsx'
wb = openpyxl.load_workbook(fn)
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])
break
ws = wb.get_sheet_by_name(sheet_selected.title)
print(ws)
########### 资产负债表 1年初存货
for row in ws.iter_rows():
for cell in row:
if str(str(cell.value).replace(' ', '')).find("总计") != -1:
qiyeshu_row = cell.row
else:
ws_qiyeshu = 0
break
for row in ws.iter_rows():
for cell in row:
if str(str(cell.value).replace(' ', '')).find("企业数") != -1 :
qiyeshu_column = cell.column
break
if qiyeshu_row == 0 or qiyeshu_column == 0:
ws_qiyeshu = 0
else:
if ws.cell(row=qiyeshu_row, column=qiyeshu_column).value != None:
ws_qiyeshu = ws.cell(row=qiyeshu_row, column=qiyeshu_column).value
print(ws_qiyeshu)
for row in ws.iter_rows():
for cell in row:
if str(str(cell.value).replace(' ', '')).find("总计") != -1:
zichanzongji_row = cell.row
else:
ws_zichanzongji = 0
break
for row in ws.iter_rows():
for cell in row:
if str(str(cell.value).replace(' ', '')).find("资产总计") != -1 :
zichanzongji_column = cell.column
break
if zichanzongji_row == 0 or zichanzongji_column == 0:
ws_zichanzongji = 0
else:
if ws.cell(row=zichanzongji_row, column=zichanzongji_column).value != None:
ws_zichanzongji = ws.cell(row=zichanzongji_row, column=zichanzongji_column).value
print(ws_zichanzongji)
财务季报---重在内容02
最新推荐文章于 2024-07-10 10:05:08 发布
本文介绍使用Python的openpyxl库从Excel文件中定位并提取特定数据的方法。通过示例代码展示了如何打开指定的工作簿,查找包含关键词的工作表,并获取单元格的具体数值。
5779

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



