我试图从脚本运行Windows帮助文件编译器( hhc.exe
),但它显示非常意外的行为。
当我从cmd.exe与hhc pathtohelpproject.hpp
运行它,帮助文件编译按预期。 然而,从python调用完全相同的命令与相同的工作目录导致程序返回0,并没有输出。
但它变得更奇怪:我创build了一个batch filerunhhc.bat
:
hhc "pathtohelpproject.hpp"
我通过调用call runhhc.bat
从python脚本call runhhc.bat
, start runhhc.bat
和runhhc.bat
。
所有这些都导致了相同的行为。 但是, start runhhc.bat
,cmd实例在hhc返回后仍然打开,所以我尝试再次手动input命令,但没有成功。 但是当我刚刚手动打开cmdinput命令时,它也不起作用! 事实上,一旦我closures了由我的脚本打开的cmd,它才开始工作。
这个奇怪的行为有什么解释? 我怎样才能不pipe脚本运行编译器?
这完全取决于hhc.exe,没有别的。 诀窍是在运行后查看%ERRORLEVEL%。 尽管成功,它返回“1”。 如果hhc.exe运行与其他东西隔离,这可以用于自定义命令来警告用户它是假的。 HHC.exe正在使用HHA.dll。 关于HHA.dll信息尚未发布。 Microsoft根据保密协议(NDA)将HHA界面信息授予经批准的帮助ISV。
D:\_working>"C:\Program Files (x86)\HTML Help Workshop\hhc" foobar.hhp Microsoft HTML Help Compiler 4.74.8702 Compiling d:\_working\foobar.chm ... Compile time: 0 minutes, 1 second 22 Topics 87 Local links 2 Internet links 0 Graphics Created d:\_working\foobar.chm, 305,338 bytes Compression decreased file by 53,639 bytes. D:\_working>echo %errorlevel% 1
要解决这个问题并继续,你需要在批处理文件中加入if not %errorlevel% 1 exit /B 1
。
@echo off REM ----------------------------------------- REM batch file is located in D:\_batch REM HH project file is located in D:\_working REM ----------------------------------------- cd ..\_working echo '//--- HH Compiler start -------------------------------------- "C:\Program Files (x86)\HTML Help Workshop\hhc" foobar.hhp echo '//--- HH Compiler end -------------------------------------- echo '//--- errorlevel ------------------------------------------- echo %errorlevel% echo '//------------------------------------------------------------ if not %errorlevel% 1 exit /B 1
和一个调用这个批处理的Python脚本:
print ("*******************************") print ("We compile a CHM help file ...") print ("*******************************") # First import the 'ctypes' module. ctypes module provides C compatible data types and allows calling functions in DLLs or shared libraries. import ctypes # An included library with Python install. # ctypes.windll.user32.MessageBoxW(0, "Open CHM", "Your title", 1) # OK only messageBox = ctypes.windll.user32.MessageBoxA # documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx returnValue = messageBox(None,"Compile Help modulee (CHM) now?","CHM and Python",1) # 1=OK Cancel, 2=Cancel, Retry, Ignore if returnValue == 1: print("Pressed OK") # How to compile a chm file in Python? # --------------------------------- import os os.system("D:/_batch/run-hhc.bat") elif returnValue == 2: print("user pressed cancel button!")
您可能有兴趣从Python脚本中调用CHM:
# How to open a chm file in Python? # --------------------------------- # os.system("hh.exe D:/UserData-QGIS-Python/Projekte/ConnectChm/CHM-example.chm") import os os.system("hh.exe D:/UserData-QGIS-Python/Projekte/ConnectChm/CHM-example.chm::/garden/garden.htm")