I'm trying to use the following code to convert a xlsb file into an xlsx:
import os
EXCELCNV_PATH = "\"C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCELCNV.EXE\""
FILE_PATH = "\"C:\\Users\\Tomas\\Desktop\\fix_excelcnv\\Book1"
cmd = "cmd /c " + EXCELCNV_PATH
cmd = cmd + " " + FILE_PATH + ".xlsb\" " + FILE_PATH +".xlsx\""
print(cmd)
os.system(cmd)
and I'm getting this error message "'C:\Program' is not recognized as an internal or external command, operable program or batch file.". I'm using quotes around the path, which I thought would fix the issue but apparently not, it interessting to note that if I use only the first path with the same structure:
import os
EXCELCNV_PATH = "\"C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE\""
FILE_PATH = "\"C:\\Users\\Tomas\\Desktop\\fix_excelcnv\\Book1"
cmd = "cmd /c " + EXCELCNV_PATH
#cmd = cmd + " " + FILE_PATH + ".xlsb\" " + FILE_PATH +".xlsx\""
print(cmd)
os.system(cmd)
excel is opened fine, so I don't think the problem is with the way the path is being written, but there is definitely something weird going on here, can someone understand how I could make this work?
I already tried using the process module instead of os.system, I tried using different types of quotes, I tried printing the cmd and running the command myself on the terminal (and it worked, which makes it seem even more strange that it isn't working now...). I've tried everything I could think of...
Your script generates the following command
which should give you the same error even when ran manually in the terminal (my guess is that you ran the command without the preceding
cmd /cwhich is why you thought it worked).The main problem lies in
cmd /c, which is explained in the help provided bycmd /?:So you either remove
cmd /cif you do not need it, or enclose your entire command (a sequence of 3 strings) into a string itself:Don't worry if it looks weird, cmd will parse it correctly.
Also, your command doesn't seem to do what you claimed - "convert a xlsb file into an xlsx".
excelcnv.exeis not well documented but it seems you are missing a-oiceflag, so perhaps try the following command instead:To build this command in Python it would be better to do what @picobit suggested by using
pathlibespecially for more complicated scripts, but here I'll provide a code similar to yours with the addition offandrstrings for better readability: