Printing from python on Windows platform

Paulo Soares psoares at consiste.pt
Wed Aug 4 06:36:19 EDT 1999


I suppose that you are trying to send bytes directly to the printer,
by-passing any windows printer driver. I use the following code (from
C/C++) and if I have the time I'll put it into a python extension. Note
that this code is NOT thread safe and was done to allow Visual Basic to
send data to the printer.


enum {OPEN_PRINTER, WRITE_PRINTER, CLOSE_PRINTER, ABORT_PRINTER};

extern "C"
__declspec(dllexport) long __stdcall drtprint(long action, LPCSTR text,
long length)
{
    static HANDLE hPrinter = NULL;
    switch (action)
    {
    case OPEN_PRINTER:
        if (hPrinter)
            return -1;
        if (OpenPrinter((char *)text, &hPrinter, NULL))
        {
            DOC_INFO_1 Info;
            PRINTER_INFO_2 *pp2 = NULL;
            DWORD Wrn;
            GetPrinter(hPrinter, 2, (LPBYTE)pp2, 0, &Wrn);
            BYTE *pp = new BYTE[Wrn + 1];
            GetPrinter(hPrinter, 2, (LPBYTE)pp, Wrn + 1, &Wrn);
            pp2 = (PRINTER_INFO_2 *)pp;
            char    DocName[128], OutputFile[128], DataType[128];
            memset(&Info, 0, sizeof(Info));
            strcpy(DocName, "Impressão");
            strcpy(OutputFile, pp2->pPortName);
            strcpy(DataType, "");
            Info.pDocName       = DocName;
            Info.pOutputFile    = OutputFile;
            Info.pDatatype      = 0;
            delete [] pp;
            if (!StartDocPrinter(hPrinter, 1, (LPBYTE)&Info))
            {
                DWORD err = GetLastError();
                ClosePrinter(hPrinter);
                hPrinter = NULL;
                return err;
            }
            return 0;
        }
        else
        {
            DWORD err = GetLastError();
            hPrinter = NULL;
            return err;
        }
        break;
    case WRITE_PRINTER:
        if (!hPrinter)
            return -1;
        {
            DWORD Written;
            if (!WritePrinter(hPrinter, (void *)text, length, &Written))
            {
                DWORD err = GetLastError();
                ClosePrinter(hPrinter);
                hPrinter = NULL;
                return err;
            }
            return 0;
        }
        break;
    case CLOSE_PRINTER:
        if (!hPrinter)
            return -1;
        {
            DWORD err = 0;
            if (!EndDocPrinter(hPrinter))
            {
                err = GetLastError();
            }
            ClosePrinter(hPrinter);
            hPrinter = NULL;
            return err;
        }
        break;
    case ABORT_PRINTER:
        if (!hPrinter)
            return -1;
        {
            DWORD err = 0;
            if (!AbortPrinter(hPrinter))
            {
                err = GetLastError();
            }
            ClosePrinter(hPrinter);
            hPrinter = NULL;
            return err;
        }
        break;
    }
    return -1;
}

Best Regards,
Paulo Soares

> -----Original Message-----
> From:	Johann Spies [SMTP:jhspies at futurenet.co.za]
> Sent:	Tuesday, August 03, 1999 22:26
> To:	Python-poslys
> Subject:	Printing from python on Windows platform
> 
> I am sorry to ask this a second time, but I did not receive any hint 
> after the first time I put this question to the list. 
> 
> I am a Linux user writing a program for somebody that will use it on a
> Windows platform.  In Linux I use the following to redirect printing
> to
> the printer:
> 
>     drukker = sys.stdout
>     drukker = os.popen('lpr','w')
>     drukker.write(drukstuk)
>     sys.stdout.write(drukstuk)
> 
> What would the windows equivalent be?
> 
> I have tried os.popen('prn', 'w'), but without success.  I cannot see
> anything in the python documentation to help me. 
> 
> 
> Johann
> 
> 
> ----------------------------------------------------------------------
> ----
> | Johann Spies                                 Windsorlaan 19
> |
> | jhspies at futurenet.co.za                3201 Pietermaritzburg
> |
> | Tel/Faks Nr. +27 331-46-1310		       Suid-Afrika
> (South Africa)  |
> 
> ----------------------------------------------------------------------
> ----
> 
>      "Have not I commanded thee? Be strong and of a good 
>       courage; be not afraid, neither be thou dismayed; for 
>       the LORD thy God is with thee whithersoever thou 
>       goest."                        Joshua 1:9 
> 
> 




More information about the Python-list mailing list