Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:19:17+00:00 2026-06-18T06:19:17+00:00

I need to print some information directly (without user confirmation) and I’m using Python

  • 0

I need to print some information directly (without user confirmation) and I’m using Python and the win32print module.

I’ve already read the whole Tim Golden win32print page (even read the win32print doc, which is small) and I’m using the same example he wrote there himself, but I just print nothing.

If I go to the interactive shell and make one step at a time, I get the document on the printer queue (after the StartDocPrinter), then I get the document size (after the StartPagePrinter, WritePrinter, EndPagePrinter block) and then the document disappear from the queue (after the EndDocPrinter) without printing.

I’m aware of the ShellExecute method Tim Golden showed. It works here, but it needs to create a temp file and it prints this filename, two things I don’t want.

Any ideas? Thanks in advance.

This is the code I’m testing (copy and paste of Tim Golden’s):

import os, sys  
import win32print
import time
printer_name = win32print.GetDefaultPrinter()
if sys.version_info >= (3,):
    raw_data = bytes ("This is a test", "utf-8")
else:
    raw_data = "This is a test"

hPrinter = win32print.OpenPrinter (printer_name)
try:
    hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data", None, "RAW"))
    try:
        win32print.StartPagePrinter (hPrinter)
        win32print.WritePrinter (hPrinter, raw_data)
        win32print.EndPagePrinter (hPrinter)
    finally:
        win32print.EndDocPrinter (hPrinter)
finally:
    win32print.ClosePrinter (hPrinter)

[EDIT]

I installed a pdf printer in my computer to test with another printer (CutePDF Writer) and I could generate the test of raw data.pdf file, but when I look inside there is nothing. Meaning: all commands except WritePrinter appears to be doing what they were supposed to do. But again, as I said in the comments, WritePrinter return the correct amount of bytes that were supposed to be written to the printer. I have no other idea how to solve this, but just comproved there is nothing wrong with my printer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:19:18+00:00Added an answer on June 18, 2026 at 6:19 am

    I’m still looking for the best way to do this, but I found an answer that satisfy myself for the problem that I have. In Tim Golden’s site (linked in question) you can find this example:

    import win32ui
    import win32print
    import win32con
    
    INCH = 1440
    
    hDC = win32ui.CreateDC ()
    hDC.CreatePrinterDC (win32print.GetDefaultPrinter ())
    hDC.StartDoc ("Test doc")
    hDC.StartPage ()
    hDC.SetMapMode (win32con.MM_TWIPS)
    hDC.DrawText ("TEST", (0, INCH * -1, INCH * 8, INCH * -2), win32con.DT_CENTER)
    hDC.EndPage ()
    hDC.EndDoc ()
    

    I adapted it a little bit after reading a lot of the documentation. I’ll be using win32ui library and TextOut (device context method object).

    import win32ui
    # X from the left margin, Y from top margin
    # both in pixels
    X=50; Y=50
    multi_line_string = input_string.split()
    hDC = win32ui.CreateDC ()
    hDC.CreatePrinterDC (your_printer_name)
    hDC.StartDoc (the_name_will_appear_on_printer_spool)
    hDC.StartPage ()
    for line in multi_line_string:
         hDC.TextOut(X,Y,line)
         Y += 100
    hDC.EndPage ()
    hDC.EndDoc ()
    

    I searched in meta stackoverflow before answering my own question and here I found it is an encouraged behavior, therefore I’m doing it. I’ll wait a little more to see if I get any other answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort then print the result increasing and decreasing. I have some
I need to use an awk script to extract some information from a file.
I need to know each time a Mac print job is created/completed, plus some
I'm using Python's minidom library to try and manipulate some XML files. Here is
I am using python, and suppose i had some code as below example.py import
I need some expert information about ActiveX and some valuable experience reports from those
I need some help regarding using argparse. What I want to achieve is that
I'm actually using JQueryUI Dialog plugin to show some information to the users of
I'm developing a library to IOS, which at some point need to create a
At some point I need to display a disabled (greyed out by disabled=disabled attribute)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.