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
cmd /c "C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsb" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsx"
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 /c
which is why you thought it worked).
The main problem lies in cmd /c
, which is explained in the help provided by cmd /?
:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
1. If all of the following conditions are met, then quote characters
on the command line are preserved:
- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.
2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
So you either remove cmd /c
if you do not need it, or enclose your entire command (a sequence of 3 strings) into a string itself:
cmd /c ""C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsb" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsx""
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.exe
is not well documented but it seems you are missing a -oice
flag, so perhaps try the following command instead:
cmd /c ""C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE" -oice "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsb" "C:\Users\Tomas\Desktop\fix_excelcnv\Book1.xlsx""
To build this command in Python it would be better to do what @picobit suggested by using pathlib
especially for more complicated scripts, but here I'll provide a code similar to yours with the addition of f
and r
strings for better readability:
import os
EXCELCNV_PATH = r'"C:\Program Files\Microsoft Office\root\Office16\EXCELCNV.EXE"'
FILE_PATH_STEM = r'C:\Users\Tomas\Desktop\fix_excelcnv\Book1'
XLSB_FILE_PATH = f'"{FILE_PATH_STEM}.xlsb"'
XLSX_FILE_PATH = f'"{FILE_PATH_STEM}.xlsx"'
convert_cmd = f'{EXCELCNV_PATH} -oice {XLSB_FILE_PATH} {XLSX_FILE_PATH}'
cmd = f'cmd /c "{convert_cmd}"'
print(cmd)
os.system(cmd)