A memo for creating an Excel operation tool for work. Derivation of this ↓. Get all xlsx filenames in a folder in a natural sort order and build an absolute path
Create two new folders on your desktop with the names "folder1" and "folder2".
In "folder1", throw all the xlsx files you want to convert.
Converted to a csv file and saved in "folder2".
The execution environment and the version of each library are as follows.
import os import csv import openpyxl from natsort import natsorted #Arbitrary folder name created on the desktop folder_name = "folder1" folder_2_name = "folder2" #constant desktop_path = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Desktop" folder_path = os.path.join(desktop_path, folder_name) folder_2_path = os.path.join(desktop_path, folder_2_name) #Get Excel file name list in natural order files = natsorted(os.listdir(folder_path)) #Build the absolute path of each file by turning the file name list with a for statement for filename in files: filepath = os.path.join(folder_path, filename) #Access xlsx file → Get the object of the first sheet wb = openpyxl.load_workbook(filepath) ws_name = wb.sheetnames ws = wb[ws_name] #Convert to csv and save in folder2 savecsv_path = os.path.join(folder_2_path, filename.rstrip(".xlsx")+".csv") with open(savecsv_path, 'w', newline="") as csvfile: writer = csv.writer(csvfile) for row in ws.rows: writer.writerow([cell.value for cell in row])