使用 win32 和 python 直接打印时隐藏文本文件上的页眉和页脚

发布于 2024-11-27 07:40:53 字数 202 浏览 1 评论 0原文

当使用此代码直接打印到文本文件时,我被困在这里,

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

它总是打印标题“datafile.txt”和页脚“Page1”。我想在连续表格纸上打印时隐藏或删除它。我不想安装其他第三方软件。请帮我。谢谢。

I am stuck here when printing directly to text files using this code

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

it always print the header "datafile.txt" and footer "Page1". I want to hide or remove this when printing on continous form paper. I do not want to install another third party software. Please help me. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2024-12-04 07:40:53

我相信您只需进行简单的搜索,就可以找到一个比这个hack更好地处理这个问题的模块(例如使用 Reportlab 和 ShellExecute the PDF)。此外,Windows 上打印文本文件的默认应用程序是记事本。如果您想永久配置页眉/页脚设置,只需在“文件”->“页面设置”中更改即可。

如果您希望更改程序中记事本的设置,可以使用 winreg 模块(Python 2 中的_winreg)。但是,由于 ShellExecute 不会等待作业排队,因此存在计时问题。在恢复旧设置之前,您可以休眠一会儿或等待用户输入继续。下面是一个演示该过程的快速函数:

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)

I'm sure you're just a simple search away from finding a module that will handle this problem much better than this hack (e.g. use Reportlab and ShellExecute the PDF). Also, the default application for printing text files on Windows is Notepad. If you wish to configure the header/footer setting permanently, just change it in File->Page Setup.

If you wish to change Notepad's settings in your program, you can use the winreg module (_winreg in Python 2). However, there's a timing issue since ShellExecute doesn't wait for the job to be queued. Before restoring the old setting, you can either sleep for a little while or just wait for user input to continue. Here's a quick function that demonstrates the process:

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])

You can temporarily delete the header/footer settings with the following call:

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

You can change as many of its registry settings as you want:

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)
躲猫猫 2024-12-04 07:40:53

使用这种方法似乎可以给程序员更多的控制权,当然也不会' t 在没有明确声明的情况下插入页眉和页脚。

Using this method seems to give more control to the programmer and certainly doesn't insert headers and foots without explicit statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文