目录
1.1 效果演示
1.2 源代码
#encoding: utf-8
from ctypes import *
import time
import win32com.client as win32
import os
def get_path():
path1 = input("输入路径:")
path2 = path1+r"\target files"
if not os.path.exists(path2):
os.makedirs(path2)
# print("temp file path :{}, target file path :{}".format(path1, path2))
return path1,path2;
def transform(input_path, out_path):
fileList = os.listdir(input_path) #文件夹下面所有的文件
print('程序正在运行中...')
print("正在处理:{}.".format(fileList))
for i in range(len(fileList)):
file_Name = os.path.splitext(fileList[i]) #文件和格式分开
# print("file_Name:{} \n".format(file_Name))
transfile1 = input_path + '\\' + fileList[i] # 要转换的excel
transfile2 = out_path + '\\' + file_Name[0] # 转换出来excel
if os.path.isdir(transfile1):#跳过目录不处理。
continue
if file_Name[1] == '.xls':
excel = win32.gencache.EnsureDispatch('excel.application')
pro = excel.Workbooks.Open(transfile1) # 打开要转换的excel
print("{}正在转换处理中..".format(transfile1))
pro.SaveAs(transfile2 + ".xlsx", FileFormat=51) # 另存为xlsx格式
pro.Close()
excel.Application.Quit()
elif file_Name[1] == '.csv':
excel = win32.gencache.EnsureDispatch('excel.application')
pro = excel.Workbooks.Open(transfile1) # 打开要转换的excel
print("{}正在转换处理中..".format(transfile1))
pro.SaveAs(transfile2 + ".xlsx", FileFormat=51)
pro.Close()
excel.Application.Quit()
if __name__=='__main__':
print('欢迎使用文件格式转换器。本软件可实现xls文件和csv文件批量转换为xlsx文件,以达到压缩文件体积的目的。')
path1, path2 = get_path()
start_time = time.time();
transform(path1,path2)
# print("程序已完成,共耗时 --- %.3s seconds ---" % (time.time() - start_time))
print("\n程序运行结束,共耗时{:.3f}秒...".format(time.time()-start_time))
input("按下任意按键退出本程序...")