python xls batch conversion to xlsx

 

 

 

import them

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():

    # Open the windows window and select a folder

    return eg.diropenbox()

 

if __name__ == "__main__":

    package = pick_package()

    files = os.listdir(package)

    for fname in files:

        if fname.endswith(".xls"):

            print(fname + "The format conversion is in progress, please wait~")

            try:

                currentfile = package + "\\" + fname

                save_as_xlsx(currentfile)

                print(currentfile + "format conversion completed, O(∩_∩)O haha~")

            except:

                print(currentfile + "Format conversion exception, ┭┮﹏┭┮")

        else:

            print("Skip non-xls files:" + fname)

input("Enter any key to exit")

 

 

import them

import win32com.client as win32

import easygui as eg

 

def pick_package():

    # Open the windows window and select a folder

    return eg.diropenbox()

 

if __name__ == "__main__":

    package = pick_package()

    files = os.listdir(package)

 

 

import win32com.client as win32

 

fname = "C:\\Users\\laiwu\\PycharmProjects\\pythonProject1\\income statement"

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()

 

 

"""

Use python to obtain the specified type file excel in a certain path folder and subfolders in batches, and store them according to the specified path

"""

 

import them

import shutil

 

file_excel = []

 

path = os.getcwd()

path_listdir = os.listdir(path)

 

for s in path_listdir:

    path_excel = os.path.join(path, s) # Add the file name to the current file path

    if os.path.isfile(path_excel): # If it is a file

 

 

        if os.path.splitext(path_excel)[1] == ".xlsx": # If the file has a suffix of ".pdb"

            file_excel.append(path_excel)

      #  elif os.path.splitext(path_excel)[1] == ".xls":

       #     path_excel = path_excel + 'x'

      #      file_excel.append(path_excel)

       #     print(path_excel)

 

 

 

    elif os.path.isdir(path_excel): # If it is a path

        continue

 

 

for i in range(len(file_excel)):

 

    path_dir = os.path.split(file_excel[i])[0]

  #  print(path_dir)

    path_file = os.path.split(file_excel[i])[1]

  #  print(path_file)

    filename = path_dir + '/' + path_file

    filename_bak = path + '/ bak /' + path_file

    shutil.copyfile(filename, filename_bak)

 

Guess you like

Origin blog.csdn.net/jidawanghao/article/details/112522686