我有几个速查表:
Sheet 1 sheet2 sheet3
A B C D E F D F G
1 2 3 4 5 6 7 9 8我使用pyexcel将电子表格1和2以及1和3中的行连接在一起,因此1和2的合并行将为:
A B C D E F D F G,
1 2 3 4 5 6 和1和3:
A B C D E F D F G
1 2 3 7 9 8如何在pyexcel中做到这一点?
现在我有两个for循环和这个:
if t_row['name'] is not "":
update_sheet[count, 'name'] = t_row['name']但是工作表2没有F和G列,工作表3没有E和F,我如何列出工作表有哪些列,或者只是取整行并将其与row连接并存储?
发布于 2016-07-21 13:44:35
目前还不清楚:
将numpy导入为np导入pyexcel为pe a= np.array(pe.get_array(file_name='Sheet1.xlsx')) b= np.array(pe.get_array(file_name='Sheet2.xlsx')) c= np.array(pe.get_array(file_name='Sheet3.xlsx')) all=a,b,对于范围(3)中的i,c max_cols = max([i.shape1 in i]):if alli.dtype!=np.dtype('int'):allialli=='']=0 alli=alli.astype('int') if (alli.shape1 != max_cols):alli=np.hstack([alli,[*(max_cols-ali.shape1)]*(alli.shape)]) np.sum(np.vstack(all),0)
编辑
使用时不需要for循环(仅用于在不同的工作表中循环)。这将以一种蟒蛇的方式使用numpy!
def join_sheets(a, b):
both = [a,b]
max_cols = max([i.number_of_columns() for i in both])
min_rows = min([i.number_of_rows() for i in both])
both_arr = [np.array(i.array) for i in both]
for i in range(2):
both_arr[i] = np.hstack([both_arr[i], [['']*(max_cols - both_arr[i].shape[1])]*(both_arr[i].shape[0])])
both_arr[0][0:min_rows,][both_arr[1][0:min_rows,]!=''] = both_arr[1][0:min_rows,][both_arr[1][0:min_rows,]!='']
if (b.number_of_rows() > min_rows):
both_arr[0] = np.vstack([both_arr[0], both_arr[1][min_rows:,]])
a.array = both_arr[0].tolist()
sheets = pe.get_book(file_name='Sheet1.xlsx')
for i in range(1, sheets.number_of_sheets()): join_sheets(sheets[0], sheets[i])
sheets.save_as(sheets.path + '/' + sheets.filename)https://stackoverflow.com/questions/38494652
复制相似问题