在python中通过图像发送到打印机上的文本

时间:2012-11-08 10:19:10

标签: python pywin32

在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格式)的方法或模块。

2 个答案:

答案 0 :(得分:2)

我认为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")

结果:

enter image description here

答案 1 :(得分:0)

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

请参阅此答案:https://stackoverflow.com/a/4498956/3571