import openpyxl
import pandas as pd
import os
import shutil
# 判断文件夹是否存在,不存在则新建,存在则清空
def clean_file(file_name):
if os.path.isdir(file_name):
shutil.rmtree(file_name)
os.makedirs(file_name)
else:
os.makedirs(file_name)
def save_as_excel(old_file, new_file):
clean_file(new_file)
list_excel = os.listdir(old_file)
for excel_name in list_excel:
if '.xlsx' in excel_name.lower():
excel_name = excel_name.lower()
old_excel = old_file + '\\' + excel_name
new_excel = new_file + '\\' + excel_name
work_book = openpyxl.load_workbook(old_excel)
writer = pd.ExcelWriter(new_excel, engine='openpyxl')
writer.book = work_book
writer.save()
writer.close()
print(f'已保存: {new_excel}')
if __name__ == '__main__':
old_file = r'E:\博客\另存Excel\原始文件'
new_file = r'E:\博客\另存Excel\另存文件'
save_as_excel(old_file, new_file)