PyCharm+Python3转换xls文件为xlsx文件格式

    科技2022-08-06  92

    import os import os.path import win32com.client as win32 """ ======================================== 主函数功能测试 ======================================== """ if __name__ == '__main__': xlsFilePath = os.path.join(os.getcwd() + "\\" + "通知单.xls") # 文件存在就加载,不存在就提示检查。 if os.path.exists(xlsFilePath): # 文件存在就加载,加载失败就报错。 try: excel = win32.gencache.EnsureDispatch('Excel.Application') wb = excel.Workbooks.Open(xlsFilePath) # FileFormat = 51 is for .xlsx extension # FileFormat = 56 is for .xls extension wb.SaveAs(xlsFilePath + "x", FileFormat=51) wb.Close() excel.Application.Quit() os.remove(xlsFilePath) # 删除老的xls文件 except: print("加载xls文件失败!请检查!") raise else: print("加载的xls文件不存在!请检查!")
    Processed: 0.020, SQL: 8