文章目录
代码展示:
该代码主要用于简单的批量提取excel中,岩心数据,因为重复度高,所以没有使用正则化去自动提取,先是确定固定的下标,然后通过下标来提取每一口井的岩心数据。
#导入pands包,用来读取excel数据
#导入load_workbook包,用来对excel写入数据
import pandas as pd
import numpy as np
from openpyxl import load_workbook
def append_add(A,B):
"""将数组拼接"""
C = np.append(A,B)
return C
def dic1(j):
'''确定要提取的属性'''
if j == 1:
a =np.array(data.loc[5,0])
b =np.array(data.loc[6:13,0])
c =np.array(data.loc[6:13,7])
# ff=np.array(data.loc[5:10,14].dropna(axis=0))
return a,b,c
#写入数据
df=pd.DataFrame()#构造原始数据文件
df.to_excel('3.xlsx')
j=1
for i in range(2,1000):
'''遍历每一个sheet,从而提取该sheet上对应的值'''
data = pd.read_excel("NB19-6-5Sa井高压压汞分析报告(10个岩心).xlsx",sheet_name=i,header=None,index_col=False)
# print(data)
a =np.array(data.loc[5,1])
b =np.array(data.loc[6:13,3])
c =np.array(data.loc[6:14,11])
A=[] #设置一个空列表
if j==1:
aa,bb ,cc= dic1(j)
sum_and=[aa,bb,cc,a,b,c]
for i in sum_and:
A = append_add(A,i)
j=j-1
A = A.reshape(2,-1).T
else:
A=[]
sum_and = [a,b,c]
for i in sum_and:
A = append_add(A,i)
A = pd.DataFrame(A)
#要写入的数据
df1 = pd.DataFrame(pd.read_excel('3.xlsx'))
df_row = df1.shape[1]
with pd.ExcelWriter('3.xlsx',mode='a') as writer:
book = load_workbook('3.xlsx')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
A.to_excel(writer,sheet_name='Sheet1',index=False,header=False,startcol=df_row)
print(df_row)