excel 文件路径中的os.system()空格-无法识别“C:\Program”

wgmfuz8q  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(127)

我尝试使用以下代码将xlsb文件转换为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)

字符串
我得到这个错误消息“'C:\Program' is not recognized as an internal or external command,operable program or batch file.".我在路径周围使用引号,我认为这会解决问题,但显然不是,它interesting 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打开得很好,所以我不认为问题出在路径的编写方式上,但是这里肯定有一些奇怪的事情,有人能理解我是如何做到这一点的吗?
我已经尝试使用进程模块而不是os.system,我尝试使用不同类型的引号,我尝试打印cmd并在终端上自己运行命令(它工作,这使得它似乎更奇怪,它现在不工作.).我已经尝试了我能想到的一切.

8i9zcol2

8i9zcol21#

您的脚本将生成以下命令

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"

字符串
这应该给你给予同样的错误,即使在终端中手动运行(我的猜测是,你运行的命令没有前面的cmd /c,这就是为什么你认为它工作)。
主要的问题在于cmd /c,在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.


因此,如果不需要,可以删除cmd /c,或者将整个命令(3个字符串的序列)包含在字符串中:

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""


不要担心它看起来很奇怪,cmd会正确地解析它。
此外,您的命令似乎没有做到您声称的那样-“将xlsb文件转换为xlsx”。
excelcnv.exe没有很好的文档记录,但似乎你缺少了一个-oice标志,所以也许可以尝试以下命令:

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""


要在Python中构建此命令,最好按照@picobit的建议使用pathlib,特别是对于更复杂的脚本,但在这里我将提供一个类似于您的代码,添加fr字符串以提高可读性:

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)

相关问题