Skip to content

Instantly share code, notes, and snippets.

@karntrehan
Last active May 19, 2023 06:42
Show Gist options
  • Save karntrehan/f7402322ae24495238f7c944a33cba85 to your computer and use it in GitHub Desktop.
Save karntrehan/f7402322ae24495238f7c944a33cba85 to your computer and use it in GitHub Desktop.
import os
import pathlib
from subprocess import PIPE, run
def renameFile(file, root) -> str:
new_filename = file.replace(" ", "")
os.rename(os.path.join(root, file), os.path.join(root, new_filename))
return new_filename
def folderFromRoot(root) -> str:
splits = root.split("/")
directory = splits[-2:]
return "/".join(directory)
def generateSpecialization(splits, indexToStart) -> str:
result = ""
for split in splits[indexToStart:]:
result += split.capitalize()+"_"
return result[:-1]
def outputName(filename) -> str:
splits = filename.split("_")
course = splits[1]
year = splits[2]+"_"+splits[3]
addSpecialisation = False
procedureIndex = 4
if "Pediatric" in filename or "Midwifery" in filename:
addSpecialisation = True
specialization = splits[4]
procedureIndex = 5
procedure = generateSpecialization(splits, procedureIndex)
outputName = "outputs/"+course+"_"+year+"_"
if addSpecialisation:
outputName += specialization+"_"
outputName += procedure+".xml"
return outputName.lower()
totalFiles = 0
successCount = 0
failureCount = 0
path = pathlib.Path(__file__).parent.resolve()
filePathlist = []
fileNamelist = []
for root, dirs, files in os.walk(path):
files = [fi for fi in files if fi.endswith(".xlsx")]
for file in files:
# Run only once to rename the files.
# renameFile(file,root)
totalFiles += 1
filePath = folderFromRoot(root)+"/"+file
# Clean up the file names - Remove spaces, commas, double underscores, etc..
cleanName = file.replace(
"-", "_").replace(" ", "_").replace(",", "").replace(".xlsx", "").replace("__", "")
# Split the file name by "_" and get info from the file names
outputName = outputName(cleanName)
print("\n\nConverting file :"+outputName+" from "+filePath)
conversionResult = run(["xls2xform " + filePath + " "+outputName],
stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
if "xls2xform: error:" in conversionResult.stderr:
failureCount += 1
print("STDError: "+conversionResult.stderr)
else:
successCount += 1
print("Conversion successful")
print("Total to convert "+str(totalFiles)+" files")
print("Successfully converted "+str(successCount)+" files")
print("Failed to convert "+str(failureCount)+" files")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment