Skip to content

Instantly share code, notes, and snippets.

@anhanh11001
Created December 10, 2020 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhanh11001/58b75120ae7b75144b86416214c9649e to your computer and use it in GitHub Desktop.
Save anhanh11001/58b75120ae7b75144b86416214c9649e to your computer and use it in GitHub Desktop.
Convert xls to xlsx
import os
import xlrd
from openpyxl.workbook import Workbook as openpyxlWorkbook
def convert_file(original_file, new_file):
xlsBook = xlrd.open_workbook(original_file)
workbook = openpyxlWorkbook()
for i in range(0, xlsBook.nsheets):
xlsSheet = xlsBook.sheet_by_index(i)
sheet = workbook.active if i == 0 else workbook.create_sheet()
sheet.title = xlsSheet.name
for row in range(0, xlsSheet.nrows):
for col in range(0, xlsSheet.ncols):
sheet.cell(row=row + 1, column=col + 1).value = xlsSheet.cell_value(row, col)
print(workbook)
workbook.save(filename=new_file)
### RUN AND CONFIG HERE
folder_name = "Write folder name here"
for file_name in os.listdir(folder_name):
if not file_name.endswith(".xls"):
continue
convert_file(file_name, file_name + "x") # Change format file from xls to xlsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment