[Python] 纯文本查看 复制代码 # 合并多表.py
#zpy2@52pojie
import os
import glob
path1 = r"数据/*.xlsx"
files_result = glob.glob(path1)
#import sys
#sys.exit()
#将rows写入excel
def op_toexcel(data,filename): # openpyxl库储存数据到excel
import openpyxl as op
#wb = op.Workbook(filename) # 创建工作簿对象
wb=op.load_workbook(filename)
ws = wb['Sheet1'] # 创建子表
for i in range(len(data)):
d = data[i]
print(d)
ws.append(d) # 每次写入一行
wb.save(filename)
import openpyxl as op
lines=[]
for file in files_result:
wb=op.load_workbook(file)
sheet_name='Sheet1'
ws=wb[sheet_name]
first_row=2
last_row=30
first_column=1
last_column=8
#enterprise_name=ws.cell(2,2).value
for row in range(first_row,last_row):
#第一列不为空
if ws.cell(row,1).value is None:
break
line=[]
#line.append(enterprise_name)
for column in range(first_column,last_column):
#b.xlsx-sheet2的A列、B列、C列
#B列、E列、T列
if column==2 or column==5 or column==17:
line.append(ws.cell(row,column).value)
lines.append(line)
#print(lines)
import shutil
source='合并.xlsx.bak'
target='合并.xlsx'
#b.xlsx
#shutil.copyfile(source,target)
op_toexcel(lines,target)
'''
因为 合并.xlsx有空行
合并后,从25行写入
'''
wb=op.load_workbook(target)
ws=wb['Sheet1']
range_date=ws.values
l=[[cell for cell in row] for row in range_date][1:]
print(l)
通过一番搜索
学习写了一个 |