Skip to content

Python

Open a CHM help file

Here's a short description of how to open a CHM help file using Python.

open-chm-helpfile.py
print ("********************************")
print ("Hello Help Community!")
print ("--------------------------------")
print ("We open a CHM help file here ...")
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.
# import easygui # maybe you want to use another solution by easygui
# ctypes.windll.user32.MessageBoxW(0, "Open CHM", "Your title", 1) # OK only

def MessageBox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)

# documentation of parameters: https://docs.microsoft.com/de-de/windows/win32/api/winuser/nf-winuser-messageboxw
##  Styles:
##  0 : OK
##  1 : OK | Cancel
##  2 : Abort | Retry | Ignore
##  3 : Yes | No | Cancel
##  4 : Yes | No
##  5 : Retry | No 
##  6 : Cancel | Try Again | Continue

#returnValue = MessageBox(" CHM and Python", "Open Compiled Help Module (CHM) now?", 1) 
# returnValue = MessageBox(" CHM and Python", "Open Compiled Help Module (CHM) now?", 0x40 | 0x1)
returnValue = MessageBox(" CHM and Python", "Open Compiled Help Module (CHM) now?", 1) 


# If the function succeeds, the return value is one of the following menu-item values.
# -----------------------------------------------------------------------------------
# 1 The OK button was selected.
# 2 The Cancel button was selected.
# 3 The Abort button was selected.
# 4 The Retry button was selected.
# 5 The Ignore button was selected.
# 6 The Yes button was selected.
# 7 The No button was selected.
# 10 The Try Again button was selected.
# 11 The Continue button was selected.

if returnValue == 1:
    print("Pressed OK")
    # 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/flowers.htm")
elif returnValue == 2: 
    print("Pressed Cancel")

# import subprocess as sub
# def help(self) os.system("hh.exe d:/help.chm::/4_Userguide.htm#_Toc270510") 
# def help(self) subprocess.Popen("hh.exe d:/help.chm::/4_Userguide.htm#_Toc270510")