#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@content:实现对某个目录下的excel文件批量添加一个sheet1页保存到另外一个文件目录下
@author:zzx
@file:exceladdsheet.py
@time:2021/03/14
"""
import os,openpyxl
##对list类型的文件名是excel文件,添加一个sheet1页
def excelAddSheet(list,oldpath,addpath):
for i in list:
# print(oldpath+'\\'+i)
wb=openpyxl.load_workbook(oldpath+'\\'+i)
wb.create_sheet(title='Sheet1',index=1)
wb.save(addpath+'\\'+i)
##获取当前文件夹下的所有文件名称并返回list类型
def getFileNames(path):
List_Filename=[]
filenames = os.listdir(path)
for i, filename in enumerate(filenames):
List_Filename.append(filename)
return List_Filename;
#----分别调用---------------
# if __name__ == '__main__':
# path=r'D:\workspace_Py\xls'
# addpath=r'D:\workspace_Py\xlss'
# list=getFileNames(path)
# excelAddSheet(list,path,addpath)
#两个方法合并
def excelAddSheets(path,addpath):
list=getFileNames(path)
for i in list:
# print(oldpath+'\\'+i)
wb = openpyxl.load_workbook(path + '\\' + i)
wb.create_sheet(title='Sheet1', index=1)
wb.save(addpath + '\\' + i)
if __name__=='__main__':
path=r'D:\workspace_Py\xls'
addpath=r'D:\workspace_Py\xlss'
excelAddSheets(path,addpath)
网友评论