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

扫描二维码关注公众号,回复: 12343342 查看本文章

 

    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("输入任意键退出")

 

 

import os

import win32com.client as win32

import easygui as eg

 

def pick_package():

    # 打开windows窗口,选择一个文件夹

    return eg.diropenbox()

 

if __name__ == "__main__":

    package = pick_package()

    files = os.listdir(package)

 

 

import win32com.client as win32

 

fname = "C:\\Users\\laiwu\\PycharmProjects\\pythonProject1\\利润表"

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

 

 

"""

用python批量获取某路径文件夹及子文件夹下的指定类型文件excel,并按指定路径进行存储

"""

 

import os

import shutil

 

file_excel = []

 

path = os.getcwd()

path_listdir = os.listdir(path)

 

for s in path_listdir:

    path_excel = os.path.join(path, s)  # 将文件名加入到当前文件路径后面

    if os.path.isfile(path_excel):  # 如果是文件

 

 

        if os.path.splitext(path_excel)[1] == ".xlsx":  # 如果文件是".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):  # 如果是路径

        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)

 

猜你喜欢

转载自blog.csdn.net/jidawanghao/article/details/112522686
今日推荐