【问题标题】:Send to printer a text over an image in python在python中的图像上发送文本到打印机
【发布时间】:2012-11-08 10:19:09
【问题描述】:

在 windows 系统中使用 python、wxpython 和 sqlite。我正在尝试打印一些证书/文凭/卡片,背景中有一张图片,上面有人名/文字。

我知道使用 Pywin32 中的 win32print 打印文本的基本步骤,但是:

  1. 我不知道如何添加图像并将其设置为背景。

    while .....
    
        .....
    
        # Query sqlite rows and collumn name and set the self.text for each certificate
    
        .....
    
        # Now send to printer
    
        DC = win32ui.CreateDC()
        DC.CreatePrinterDC(win32print.GetDefaultPrinter())
    
        DC.SetMapMode(win32con.MM_TWIPS)
    
        DC.StartDoc("Certificates Job")
    
        DC.StartPage()
    
        ux = 1000
        uy = -1000
        lx = 5500
        ly = -55000
    
        DC.DrawText(self.text, (ux, uy, lx, ly),win32con.DT_LEFT)
    
        DC.EndPage()
        DC.EndDoc()
    

    此打印机代码在一个 while 循环中,根据检查条件从 sqlite 数据库中调用每个人的姓名。

  2. 所有数据库名称都打印在同一页上。如何命令打印机从数据库中为每个名称输出 1 页?

  3. 欢迎使用更简单的方法或模块来处理打印机(纸张和/或 pdf)。

【问题讨论】:

    标签: python pywin32


    【解决方案1】:

    我认为使用 WxPython 是可行的,但我知道的不足以帮助你。

    但是,您可以尝试查看Python Image Library:

    示例代码:

    import sys
    from PIL import Image,ImageDraw
    
    txt = 'C\'est mon texte!'
    txt2 = '??,??!'
    
    
    im = Image.new("RGBA",(300,200),(100,155,100))
    
    draw = ImageDraw.Draw(im)
    
    draw.text( (0,50), txt )
    draw.text( (0,100), txt2)
    del draw
    
    im.save('font.png', "PNG")
    

    结果:

    【讨论】:

    • 嗯...我现在知道如何在图像上放置文本。但是如何将其发送到默认打印机?以及如何提高分辨率/质量?
    【解决方案2】:

    我建议您改为使用 reportlab 创建 PDF,然后使用 gsprint 将该 PDF 发送到打印机。

    看到这个答案:https://stackoverflow.com/a/4498956/3571

    【讨论】:

    • 对不起。不...我不想或不认为我需要为此使用任何外部 EXE 文件。如果情况很简单,只需将虚拟 pdfprinter (如 Primopdf 或任何其他)设置为默认打印机即可。
    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2016-08-06
    • 2023-03-31
    相关资源
    最近更新 更多