from openpyxl import Workbook
import pandas as pd
import glob
import xlrd
import os
def xlsx_to_xls(filename,save_path):
df = pd.read_excel(filename)
new_filename = filename[:-1]
new_save_path=save_path+'\\'+new_filename.split('\\')[-1]
df.to_excel(new_save_path, index=False)
def xls_to_xlsx(filename,save_path):
workbook = xlrd.open_workbook(filename)
sheet = workbook.sheet_by_index(0)
workbook_new = Workbook()
sheet_new = workbook_new.active
header=['时间','入流','出流','水位,','库容','汛限水位','防洪高水位','校核洪水位']
for i in range(len(header)):
sheet_new.cell(row=1, column=i+1, value=header[i])
for row in range(2, sheet.nrows):
for col in range(sheet.ncols):
sheet_new.cell(row=row, column=col+1, value=sheet.cell_value(row, col))
new_filename = filename.split('.')[0].split('\\')[-1]
new_save_path=save_path+'\\'+new_filename.split('\\')[-1]+'.xlsx'
workbook_new.save(new_save_path)
if __name__ == "__main__":
excel_files0 = glob.glob(os.path.join(folder_path, '*.xlsx'))
excel_files1 = glob.glob(os.path.join(folder_path, '*.xls'))
folder_path = r'C:\Users\fanfu\Desktop\xls'
save_path=r'C:\Users\fanfu\Desktop\xlsx'
for excel_file in excel_files1:
xls_to_xlsx(excel_file,save_path)
print('完成')