3

このコードを使用してテキストファイルに直接印刷すると、ここで立ち往生します

win32api.ShellExecute (0, "print", "datafile.txt", None, ".", 0)

ヘッダー「datafile.txt」とフッター「Page1」を常に出力します。連続用紙に印刷するとき、これを非表示または削除したい。別のサードパーティ製ソフトウェアをインストールしたくありません。私を助けてください。ありがとう。

4

2 に答える 2

4

この問題をこのハ​​ックよりもはるかにうまく処理するモジュールを見つけるには、単純な検索を行うだけだと思います(たとえば、 Reportlabを使用して PDF を ShellExecute します)。また、Windows でテキスト ファイルを印刷するための既定のアプリケーションはメモ帳です。ヘッダー/フッターの設定を永続的に構成したい場合は、[ファイル] -> [ページ設定] で変更するだけです。

プログラムでメモ帳の設定を変更したい場合は、winreg モジュール ( _winregPython 2) を使用できます。ただし、ShellExecute はジョブがキューに入れられるのを待たないため、タイミングの問題があります。古い設定を復元する前に、しばらくスリープするか、ユーザーinputが続行するのを待つことができます。プロセスを示す簡単な関数を次に示します。

try:
    import winreg
except:
    import _winreg as winreg
import win32api

def notepad_print(textfile, newset=None):
    if newset is not None: 
        oldset = {}
        hkcu = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
        notepad = winreg.OpenKey(hkcu, r'Software\Microsoft\Notepad', 0, 
                                 winreg.KEY_ALL_ACCESS)
        for key, item in newset.items():
            oldset[key] = winreg.QueryValueEx(notepad, key)
            winreg.SetValueEx(notepad, key, None, item[1], item[0])

    #force printing with notepad, instead of using the 'print' verb
    win32api.ShellExecute(0, 'open', 'notepad.exe', '/p ' + textfile, '.', 0)

    input('once the job is queued, hit <enter> to continue')

    if newset is not None:
        for key, item in oldset.items():
            winreg.SetValueEx(notepad, key, None, item[1], item[0])

次の呼び出しを使用して、ヘッダー/フッターの設定を一時的に削除できます。

notepad_print('datafile.txt', {'szHeader' : ('', 1), 'szTrailer': ('', 1)})

レジストリ設定はいくつでも変更できます。

newset = {
  #name : (value, type)
  'lfFaceName': ('Courier New', 1), 
  'lfWeight': (700, 4),            #400=normal, 700=bold
  'lfUnderline': (0, 4), 
  'lfItalic': (1, 4),              #0=disabled, 1=enabled
  'lfStrikeOut': (0, 4), 
  'iPointSize': (160, 4),          #160 = 16pt
  'iMarginBottom': (1000, 4),      #1 inch
  'iMarginTop': (1000, 4), 
  'iMarginLeft': (750, 4), 
  'iMarginRight': (750, 4), 
  'szHeader': ('&f', 1),            #header '&f'=filename
  'szTrailer': ('Page &p', 1),      #footer '&p'=page number
}

notepad_print('datafile.txt', newset)
于 2011-08-02T05:24:45.220 に答える
0

このメソッドを使用すると、プログラマーはより多くの制御を行えるようになり、明示的なステートメントがないとヘッダーとフッターが挿入されません。

于 2011-08-02T04:15:07.400 に答える