'''Excel xls 转化 xlsx''' # 第一版 单个xls文件转换 import win32com.client as win32 fname = "C:\\Users\\hlj\\Desktop\\test\\1\\0000020000000AUD3151.xls" excel = win32.gencache.EnsureDispatch('Excel.Application') wb = excel.Workbooks.Open(fname) wb.SaveAs(fname+"x", FileFormat = 51) #FileFormat = 51 is for .xlsx extension wb.Close() #FileFormat = 56 is for .xls extension excel.Application.Quit()
'''Excel xls 转化 xlsx''' # 第二版 目录下多个xls文件转换 import os import win32com.client as win32 package = "C:\\Users\\hlj\\Desktop\\test\\1\\" def save_as_xlsx(fname): excel = win32.gencache.EnsureDispatch('Excel.Application') wb = excel.Workbooks.Open(fname) wb.SaveAs(fname+"x", FileFormat = 51) #FileFormat = 51 is for .xlsx extension wb.Close() #FileFormat = 56 is for .xls extension excel.Application.Quit() if __name__ == "__main__": files = os.listdir(package) for fname in files: if fname.endswith(".xls"): print(fname + "正在进行格式转换,请稍后~") save_as_xlsx(package + fname) print(fname + "格式转换完成O(∩_∩)O哈哈~") else: print("跳过非xls文件:"+fname)
'''Excel xls 转化 xlsx''' # 第三版 增加界面化组件,让用户自行选择文件夹 import os import win32com.client as win32 import easygui as eg def save_as_xlsx(fname): excel = win32.DispatchEx('Excel.Application') wb = excel.Workbooks.Open(fname) wb.SaveAs(fname + "x", FileFormat=51) # FileFormat = 51 is for .xlsx extension wb.Close() # FileFormat = 56 is for .xls extension excel.Application.Quit() def pick_package(): # 打开windows窗口,选择一个文件夹 return eg.diropenbox() if __name__ == "__main__": package = pick_package() files = os.listdir(package) for fname in files: if fname.endswith(".xls"): print(fname + "正在进行格式转换,请稍后~") try: currentfile = package + "\\" + fname save_as_xlsx(currentfile) print(currentfile + "格式转换完成,O(∩_∩)O哈哈~") except: print(currentfile + "格式转换异常,┭┮﹏┭┮") else: print("跳过非xls文件:" + fname) input("输入任意键退出")