|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
import openpyxl
import easygui as g
import xlrd
def insert_xlsx_file(source_file_name,aim_file_name):
wb = openpyxl.load_workbook(source_file_name)
sheet_names = wb.get_sheet_names()
for sheet_name in sheet_names:
sheet = wb[sheet_name]
rows_count = sheet.max_row
cols_count = sheet.max_column
if os.path.exists(aim_file_name):
wb2 = openpyxl.load_workbook(aim_file_name)
else:
wb2 = openpyxl.Workbook()
sheet2 = wb2.get_active_sheet()
if sheet2.max_row == 1:
j = 1
else:
j = sheet2.max_row +1
for row in sheet.iter_rows():
i = 0
for cell in row:
i += 1
sheet2.cell(row = j,column = i).value = cell.value
j += 1
wb2.save(aim_file_name)
def insert_xls_file(source_file_name,aim_file_name):
wb = xlrd.open_workbook(source_file_name)
sheets = wb.sheets()
for sheet in sheets:
rows_count = sheet.nrows
cols_count = sheet.ncols
if os.path.exists(aim_file_name):
wb2 = openpyxl.load_workbook(aim_file_name)
else:
wb2 = openpyxl.Workbook()
sheet2 = wb2.get_active_sheet()
if sheet2.max_row == 1:
j = 1
else:
j = sheet2.max_row +1
for row in sheet.get_rows():
i = 0
for cell in row:
i += 1
try:
sheet2.cell(row = j,column = i).value = cell.value
except openpyxl.utils.exceptions.IllegalCharacterError:
sheet2.cell(row = j,column = i).value = 'unknown'
j += 1
wb2.save(aim_file_name)
def combine_file():
source_dir_path = g.diropenbox('请打开你要合并的文件所在的文件夹:','请选择文件夹')
aim_file_path = g.filesavebox('请选择路径并输入文件名',default='*.xlsx')
all_files = os.listdir(source_dir_path)
os.chdir(source_dir_path)
for each_file in all_files:
if os.path.isdir(os.getcwd()+os.sep+each_file):
pass
elif os.path.splitext(each_file)[1]=='.xlsx':
insert_xlsx_file(os.getcwd()+os.sep+each_file,aim_file_path)
elif os.path.splitext(each_file)[1]=='.xls':
insert_xls_file(os.getcwd()+os.sep+each_file,aim_file_path)
else:
pass
g.msgbox('合并完毕!')
if __name__=="__main__":
combine_file()
|
|