Python I want to print the text from printer but I want to be in the middle of the paper

Viewed 83

as can be understood from the title, I want to print a .txt file from the printer so that it is in the middle of the page. I know this is possible with the win32printer library but when I read the documentation I couldn't find an exact setting for it, which margin values ​​will help me to center the text exactly? Or is there a preset setting for my averaging?


from win32printing import Printer

with Printer(linegap=1) as printer:
    printer.text("title1", font_config=font)

I tried to do with margin settings but cant find a exact solution

1 Answers

With the actual win32printing, you can change the alignment of each text with the attribute "align" to "left", "right" and "center", see the next example"

from win32printing import Printer

font= {
    "height": 35,
    "faceName":'Consolas',
}

with Printer(linegap=1,margin=(200,250,50,0), auto_page=True) as printer:
    printer.text("title1", font_config=font, align='left')

Now, when you need vertical center, you have to edit the file win32printing.py because it is not defined (see its definition).

Inside the file, there is a class named "get_print_flag". Please edit as it follows (more info https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawtext):

@classmethod
def get_print_flag(cls, align="left"):
    print_flag = win32con.DT_TOP|win32con.DT_WORDBREAK|win32con.DT_NOCLIP
    if align == "left":
        print_flag |= win32con.DT_LEFT
       #Add this line to have left-center option        
    elif align == "left-center":
            print_flag |= win32con.DT_LEFT|win32con.DT_SINGLELINE|win32con.DT_VCENTER
    elif align == "center":
            print_flag |= win32con.DT_CENTER
    elif align == "right":
            print_flag |= win32con.DT_RIGHT
    return print_flag
Related