From bkline at rksystems.com Sat Nov 4 08:27:52 2017 From: bkline at rksystems.com (Bob Kline) Date: Sat, 4 Nov 2017 08:27:52 -0400 Subject: [python-win32] datetime values in adodbapi Message-ID: The adodbapi package does not seem to handle datetime values appropriately. import adodbapi print(adodbapi.version) cursor = adodbapi.connect(...) cursor.execute("CREATE TABLE #t (i INT, d DATETIME)") cursor.execute("INSERT INTO #t VALUES (42, GETDATE())") cursor.execute("SELECT * FROM #t") row = cursor.fetchone() print("d={}".format(row.d)) cursor.execute("INSERT INTO #t VALUES (?, ?)", (43, row.d)) cursor.execute("SELECT * FROM #t WHERE i = 43") row = cursor.fetchone() print("d={}".format(row.d)) When running under Python 2.7.13, the first print statement shows that the datetime value has been correctly retrieved, preserving the precision: d=2017-11-04 07:52:18.110000 but then when the second INSERT is executed an exception is raised: pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'ADODB.Parameter', u'Application uses a value of the wrong type for the current operation.', u'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240652, -2146824867), None) So the package isn't allowing the script to use the same value it was given as a valid value for the d column by the package itself. When the script is run under Python 3.6.0, the exception is not raised, but that's only because the datetime precision has been discarded: d=2017-11-04 08:15:37 d=2017-11-04 08:15:37 I get the same behavior with adodbapi 2.6.0.6 (which I downloaded from the link which indicated (http://adodbapi.sourceforge.net/index.html) that this is the latest version) and on 2.6.0.7, which is what pip installed. I would like to help get both of these behaviors corrected (failure to store datetime values with microsecond precision in Python 2.x, as well as failure to preserve sub-second precision in retrieved values in Python 3.x). Before I dig in to do the work, though, I want to check in here to make sure patches are accepted so I don't spend time on something that's not wanted. Thanks! -- Bob Kline http://www.rksystems.com mailto:bkline at rksystems.com From planders at gmail.com Sun Nov 5 08:43:07 2017 From: planders at gmail.com (Preston Landers) Date: Sun, 05 Nov 2017 13:43:07 +0000 Subject: [python-win32] os.remove not deleting a file In-Reply-To: References: Message-ID: Generally, Windows won't let you delete a file if it's open somewhere, whether in the same process or in another process. You said the file becomes delete-able when you kill python, so I'm guessing that another part of your program (not shown) is holding the file open? You can always use something like Process Explorer [1] to determine exactly which process is holding a file lock. If it's your own Python program, look for a place that is doing a file open() call that is not using a context manager ("with" statement) or at least manually calling close(). If this block of code you showed is inside a "with open" call for that file, then that's the problem. [1] https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer On Sun, Nov 5, 2017 at 5:35 AM Henk Zevenhuizen wrote: > Hi there, > > This is my first mail to this list and i have a huge problem. > > my os.remove(filename) is not working. > > i am running python27 32 bits on a windows 10 64 bits machine > > My piece of code (with debugging statements): > > raw_input('before...') > print 73, (os.path.join(VERWERKTDIR, orgname)) > > if os.path.isfile(os.path.join(VERWERKTDIR, orgname)): > os.remove(os.path.join(VERWERKTDIR, orgname)) > raw_input('after...') > > print 'file removed' > else: > print 'no file present'' > > > if there is a file to be removed i can see the print 'file removed' > however the file is still there > > when i get the message : 'before' i can delete the file through windows > explorer and with CTRL-Z i can restore the file > then i press to continue the raw_input('before...') > > Then i get the message: "after", the file is still there and i cannot > delete the through windows explorer (permission denied) > the only way to delete the file is killing python > > I don't have the slightest idea where to look > > Has anyone any idea ?? > > thanks in advance > > Henk Zevenhuizen > Holland > > > > > > > _______________________________________________ > python-win32 mailing list > python-win32 at python.org > https://mail.python.org/mailman/listinfo/python-win32 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Sun Nov 5 12:14:34 2017 From: eryksun at gmail.com (eryk sun) Date: Sun, 5 Nov 2017 17:14:34 +0000 Subject: [python-win32] os.remove not deleting a file In-Reply-To: References: Message-ID: On Tue, Oct 31, 2017 at 1:16 PM, Henk Zevenhuizen wrote: > > Then i get the message: "after", the file is still there and i cannot delete > the through windows explorer (permission denied) > the only way to delete the file is killing python Does Explorer explicitly tell you "permission denied" or "file access denied", or is it a "sharing violation" or "file in use" error? The common case is a sharing violation (error 32). Deleting a file requires opening it with delete (DELETE) access. Whether or not that's allowed depends in part on how the file is already open. Filesystems maintain counts of the number of times a file is open; the number of opens that have read (or execute), write (or append), or delete access; and the number that share read (or execute), write (or append), or delete access. In order for an open with delete access to not fail with a sharing violation, all previous opens must share delete access. Also, if any previous open has delete access, then the current open has to share delete access. IMO, access sharing should not be called a 'lock'. It could be confused with Windows file locking, which is a completely different system. File locking does not prevent file deletion. For example, Python defaults to opening files with read and write sharing, but not delete sharing: >>> fd = os.open('test.txt', os.O_CREAT) >>> os.remove('test.txt') Traceback (most recent call last): File "", line 1, in WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'test.txt' Once the file handle is closed, os.remove (WinAPI DeleteFile) succeeds: >>> os.close(fd) >>> os.remove('test.txt') >>> os.listdir('.') [] If, on the other hand, Explorer is subsequently being denied delete access (error 5), then maybe your Python program actually has the file open with delete sharing. In this case, initially 'deleting' the file will succeed, but it won't be unlinked, and re-opening the file will fail with access denied. For example, the O_TEMPORARY flag opens a file with delete sharing: >>> fd = os.open('test.txt', os.O_CREAT | os.O_TEMPORARY) >>> os.remove('test.txt') DeleteFile succeeds this time, but in Windows that only means the file's delete disposition is set. This is a flag on the underlying file/stream control block (FCB or SCB) that tells the filesystem to unlink the file when all existing references to it have been closed. We still have a handle open, so the file is not unlinked, as you can see via listdir: >>> os.listdir('.') ['test.txt'] When the delete disposition is set, it's impossible to re-open the file for any access, even the most basic access to stat() file metadata: >>> os.stat('test.txt') Traceback (most recent call last): File "", line 1, in WindowsError: [Error 5] Access is denied: 'test.txt' The file is unlinked after the handle is closed: >>> os.close(fd) >>> os.listdir('.') [] That would have happened even without calling os.remove because O_TEMPORARY flags the opened file as delete-on-close. From vernondcole at gmail.com Mon Nov 6 09:59:06 2017 From: vernondcole at gmail.com (Vernon D. Cole) Date: Mon, 6 Nov 2017 07:59:06 -0700 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: Y On Sat, Nov 4, 2017 at 6:27 AM, Bob Kline wrote: > The adodbapi package does not seem to handle datetime values appropriately. > > import adodbapi > print(adodbapi.version) > cursor = adodbapi.connect(...) > cursor.execute("CREATE TABLE #t (i INT, d DATETIME)") > cursor.execute("INSERT INTO #t VALUES (42, GETDATE())") > cursor.execute("SELECT * FROM #t") > row = cursor.fetchone() > print("d={}".format(row.d)) > cursor.execute("INSERT INTO #t VALUES (?, ?)", (43, row.d)) > cursor.execute("SELECT * FROM #t WHERE i = 43") > row = cursor.fetchone() > print("d={}".format(row.d)) > > When running under Python 2.7.13, the first print statement shows that > the datetime value has been correctly retrieved, preserving the > precision: > > d=2017-11-04 07:52:18.110000 > > but then when the second INSERT is executed an exception is raised: > > pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, > u'ADODB.Parameter', u'Application uses a value of the wrong type for > the current operation.', u'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240652, > -2146824867), None) > > So the package isn't allowing the script to use the same value it was > given as a valid value for the d column by the package itself. > > When the script is run under Python 3.6.0, the exception is not > raised, but that's only because the datetime precision has been > discarded: > > d=2017-11-04 08:15:37 > d=2017-11-04 08:15:37 > > I get the same behavior with adodbapi 2.6.0.6 (which I downloaded from > the link which indicated (http://adodbapi.sourceforge.net/index.html) > that this is the latest version) and on 2.6.0.7, which is what pip > installed. > > I would like to help get both of these behaviors corrected (failure to > store datetime values with microsecond precision in Python 2.x, as > well as failure to preserve sub-second precision in retrieved values > in Python 3.x). Before I dig in to do the work, though, I want to > check in here to make sure patches are accepted so I don't spend time > on something that's not wanted. > > Thanks! > > -- > Bob Kline > http://www.rksystems.com > mailto:bkline at rksystems.com > _______________________________________________ > python-win32 mailing list > python-win32 at python.org > https://mail.python.org/mailman/listinfo/python-win32 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vernondcole at gmail.com Mon Nov 6 10:07:49 2017 From: vernondcole at gmail.com (Vernon D. Cole) Date: Mon, 6 Nov 2017 08:07:49 -0700 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: [Sorry everyone. My kitten stepped on the keyboard and sent out a one-letter reply.] Yes, patches are accepted and welcome. My last remaining Windows development environment (I work mostly on Ubuntu these years) died several months ago leaving me high and dry to do maintenance. I have purchased its replacement and promise to get back to work. An assistant administrator for adodbapi would be welcome, too. I am 67 and will not last forever. I also have a plan for a MSSQL Server test bed to be available for volunteers to use for testing. Bob: What SQL engine and version are you running? -- Vernon Cole On Mon, Nov 6, 2017 at 7:59 AM, Vernon D. Cole wrote: > Y > > On Sat, Nov 4, 2017 at 6:27 AM, Bob Kline wrote: > >> The adodbapi package does not seem to handle datetime values >> appropriately. >> >> import adodbapi >> print(adodbapi.version) >> cursor = adodbapi.connect(...) >> cursor.execute("CREATE TABLE #t (i INT, d DATETIME)") >> cursor.execute("INSERT INTO #t VALUES (42, GETDATE())") >> cursor.execute("SELECT * FROM #t") >> row = cursor.fetchone() >> print("d={}".format(row.d)) >> cursor.execute("INSERT INTO #t VALUES (?, ?)", (43, row.d)) >> cursor.execute("SELECT * FROM #t WHERE i = 43") >> row = cursor.fetchone() >> print("d={}".format(row.d)) >> >> When running under Python 2.7.13, the first print statement shows that >> the datetime value has been correctly retrieved, preserving the >> precision: >> >> d=2017-11-04 07:52:18.110000 >> >> but then when the second INSERT is executed an exception is raised: >> >> pywintypes.com_error: (-2147352567 <(214)%20735-2567>, 'Exception >> occurred.', (0, >> u'ADODB.Parameter', u'Application uses a value of the wrong type for >> the current operation.', u'C:\\WINDOWS\\HELP\\ADO270.CHM', 1240652, >> -2146824867 <(214)%20682-4867>), None) >> >> So the package isn't allowing the script to use the same value it was >> given as a valid value for the d column by the package itself. >> >> When the script is run under Python 3.6.0, the exception is not >> raised, but that's only because the datetime precision has been >> discarded: >> >> d=2017-11-04 08:15:37 >> d=2017-11-04 08:15:37 >> >> I get the same behavior with adodbapi 2.6.0.6 (which I downloaded from >> the link which indicated (http://adodbapi.sourceforge.net/index.html) >> that this is the latest version) and on 2.6.0.7, which is what pip >> installed. >> >> I would like to help get both of these behaviors corrected (failure to >> store datetime values with microsecond precision in Python 2.x, as >> well as failure to preserve sub-second precision in retrieved values >> in Python 3.x). Before I dig in to do the work, though, I want to >> check in here to make sure patches are accepted so I don't spend time >> on something that's not wanted. >> >> Thanks! >> >> -- >> Bob Kline >> http://www.rksystems.com >> mailto:bkline at rksystems.com >> _______________________________________________ >> python-win32 mailing list >> python-win32 at python.org >> https://mail.python.org/mailman/listinfo/python-win32 >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bkline at rksystems.com Mon Nov 6 10:09:21 2017 From: bkline at rksystems.com (Bob Kline) Date: Mon, 6 Nov 2017 10:09:21 -0500 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: On Mon, Nov 6, 2017 at 9:59 AM, Vernon D. Cole wrote: > On Sat, Nov 4, 2017 at 6:27 AM, Bob Kline wrote: >> >> The adodbapi package does not seem to handle datetime values .... > > Y You hit the Send button too soon, right? -- Bob Kline http://www.rksystems.com mailto:bkline at rksystems.com From bkline at rksystems.com Mon Nov 6 10:23:38 2017 From: bkline at rksystems.com (Bob Kline) Date: Mon, 6 Nov 2017 10:23:38 -0500 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: On Mon, Nov 6, 2017 at 10:07 AM, Vernon D. Cole wrote: > [Sorry everyone. My kitten stepped on the keyboard and sent out a one-letter > reply.] Always good to have helpful assistants. :-) > Yes, patches are accepted and welcome. My last remaining Windows > development environment (I work mostly on Ubuntu these years) died several > months ago leaving me high and dry to do maintenance. I have purchased its > replacement and promise to get back to work. An assistant administrator for > adodbapi would be welcome, too. I am 67 and will not last forever. > I also have a plan for a MSSQL Server test bed to be available for > volunteers to use for testing. I'm happy to contribute what I can, but you're younger than I am. :-) > What SQL engine and version are you running? I have reproduced this with several SQL Server versions. For example: * Microsoft SQL Server 2017 (RC2) - 14.0.900.75 (X64) Jul 27 2017 08:53:49 Developer Edition on Linux (Ubuntu 17.04) * Microsoft SQL Server 2008 R2 (SP3) - 10.50.6220.0 (X64) Mar 19 2015 12:32:14 Enterprise Edition on Windows NT 6.1 SP1 * Microsoft SQL Server 2016 (SP1) (KB3182545) - 13.0.4001.0 (X64) Oct 28 2016 18:17:30 Developer Edition on Windows Server 2012 R2 -- Bob Kline http://www.rksystems.com mailto:bkline at rksystems.com From maxslimmer at gmail.com Mon Nov 6 17:47:48 2017 From: maxslimmer at gmail.com (Max Slimmer) Date: Mon, 6 Nov 2017 14:47:48 -0800 Subject: [python-win32] datetime values in adodbapi Message-ID: Coincidentally I ran into this issue last week. From what I could gather the MS SQL data type can accept values with no more than millisecond precision. This is true using parameterized statements through adodbapi and also T-SQL and implicit conversion from char to datetime. The type accepts up to seven places (nano seconds). If you don?t mind losing some precision you can do something like this: import datetime import adodbapi class MyTimeconverter(adodbapi.pythonDateTimeConverter): def DateObjectToIsoFormatString(self, obj): ''' Round microsecond part of datetime object to three decimal places. ''' s = super(adodbapi.pythonDateTimeConverter, self).DateObjectToIsoFormatString(obj) try: dt, micros = s.rsplit('.', 1) except ValueError: return s micros = str(round(float('.' + micros), 3))[1:5] return dt + micros # Bind datetime.datetime parameters as string adodbapi.typeMap[datetime.datetime] = adodbapi.adBSTR# Patch on our datetime converter adodbapi.adodbapi.dateconverter = MyTimeconverter() Or maybe you can use datetime2. ?Max III ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From timr at probo.com Tue Nov 7 01:16:21 2017 From: timr at probo.com (Tim Roberts) Date: Mon, 6 Nov 2017 22:16:21 -0800 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: On Nov 6, 2017, at 2:47 PM, Max Slimmer wrote: > > Coincidentally I ran into this issue last week. From what I could gather the MS SQL data type can accept values with no more than millisecond precision. This is true using parameterized statements through adodbapi and also T-SQL and implicit conversion from char to datetime. The type accepts up to seven places (nano seconds). > Your post is not exactly correct, and the difference is critically important. The adodbapi happens to display time strings with 7 decimal digits, but that is TOTALLY unrelated to the precision with which the underlying database engine stores its date/time values, or the precision returned by the NOW() function in your database engine. This is not specified by the standard. By default, for example, the MySQL DATETIME time does not store fractional seconds at all. ? Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. -------------- next part -------------- An HTML attachment was scrubbed... URL: From vernondcole at gmail.com Tue Nov 7 10:11:27 2017 From: vernondcole at gmail.com (Vernon D. Cole) Date: Tue, 7 Nov 2017 08:11:27 -0700 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: The real question here is where the significance gets lost. It's hard to get a handle on what is really in the tables. Also, the time conversion routines (both in and out of the database) are a mass of confusion. Obviously Bob has found a where a value an internal form blows up when fed directly back into an INSERT. I suspect that if his row.date value was run through a real datetime object that things would magically work differently. This code has been around since long before datetime was invented, and tries to handle every possible format. Washing everything through datetime internally is probably not the answer, either, since datetime is _really_ slow -- at least it was last time I did performance tests. The code resorts to falling back to handling time values as ISO strings in some cases -- depending on a guess of how the underlying database engine will accept them. Remember that this module needs to work with *any* database engine, not just the two sold by Microsoft. Data may be lost in the conversion between COM dates, datetime dates, whatever the database stores internally, and human readable dates. I have never examined where small amounts of precision might be lost. The unit tests are loose. Tests for time.time() are even worse -- if a test worked for me in US Mountain Standard zone it was sure to fail for Mark in Australia. Ugh! But I digress. This will take some really detailed debugging. My thanks go out to whomever tackles it. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bkline at rksystems.com Tue Nov 7 11:40:15 2017 From: bkline at rksystems.com (Bob Kline) Date: Tue, 7 Nov 2017 11:40:15 -0500 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: On Tue, Nov 7, 2017 at 10:49 AM, Dennis Lee Bieber wrote: > >> When running under Python 2.7.13, the first print statement shows that > >> the datetime value has been correctly retrieved, preserving the > >> precision: > >> > >> d=2017-11-04 07:52:18.110000 > >> > Given that string of 0s, the only thing I'd be able to conclude from > the above is that the /formatting/ of the output specified a high > precision, which may not be in the data itself. The adodbapi package returns standard Python library datetime objects for non-NULL values retrieved from SQL Server DATETIME columns, so that's what row.d is. The fact that the printed representation of the value has trailing zeros reflects the default formatting for datetime objects. -- Bob Kline http://www.rksystems.com mailto:bkline at rksystems.com From bkline at rksystems.com Tue Nov 7 11:48:08 2017 From: bkline at rksystems.com (Bob Kline) Date: Tue, 7 Nov 2017 11:48:08 -0500 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: On Tue, Nov 7, 2017 at 10:11 AM, Vernon D. Cole wrote: > Obviously Bob has found a where a value an internal form blows up when fed > directly back into an INSERT. I suspect that if his row.date value was run > through a real datetime object that things would magically work differently. I'm not sure what that means. The value I get back from adodbapi from my SELECT query *is* a Python datetime object: >>> row = cursor.fetchone() >>> type(row[0]) What would be the difference between that and a "real datetime object"? -- Bob Kline http://www.rksystems.com mailto:bkline at rksystems.com From bkline at rksystems.com Wed Nov 8 08:46:08 2017 From: bkline at rksystems.com (Bob Kline) Date: Wed, 8 Nov 2017 08:46:08 -0500 Subject: [python-win32] datetime values in adodbapi In-Reply-To: References: Message-ID: On Mon, Nov 6, 2017 at 5:47 PM, Max Slimmer wrote: > If you don?t mind losing some precision you can do something like this: > ... [code to override dateconverter] ... That would address the first bug, at least for my own immediate application. It would take more digging to determine how generally applicable it would be for a patch to the package. > Or maybe you can use datetime2. Another possible solution, sidestepping the first bug altogether. Thanks for your helpful suggestions. I'll need to scratch my head further to see if I can solve the second problem (loss of precision when running under Python 3). If I can't, I may take a different route altogether, letting the application server decide what time it is instead of the database server. In this approach, I would just create a variable in Python to represent the current date/time with sub-second precision stripped (or rounded away) and store that value. That way, at the cost of some precision, I'll be able to guarantee that Python 2 and Python 3 code (any code, really) will get the same value from the database. In this particular instance, the consistency is worth more than the precision. -- Bob Kline http://www.rksystems.com mailto:bkline at rksystems.com From elitsa.hineva at abv.bg Tue Nov 14 14:00:39 2017 From: elitsa.hineva at abv.bg (Elitsa Hineva) Date: Tue, 14 Nov 2017 21:00:39 +0200 (EET) Subject: [python-win32] a question about and error In-Reply-To: <241864665.5272827.1510686000938.JavaMail.apache@nm42.abv.bg> References: <241864665.5272827.1510686000938.JavaMail.apache@nm42.abv.bg> Message-ID: <286143289.5272875.1510686039108.JavaMail.apache@nm42.abv.bg> Dear all, I am trying to run a GIS model for wind fetch calculation but it didn't work. Instead I have received the following note: : (-2147467259, 'Unspecified error', None, None) Failed to execute (fetch_jjr). What do i need to do to solve this problem? Thnk you very much in advance. Regrads, ELitsa Sincerely yours, Elitsa Hineva, "Biology and ecology of the sea" department, Institute of Oceanology, 40 "Parvi mai" str., Varna, Bulgaria http://www.io-bas.bg/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From timr at probo.com Wed Nov 15 17:26:18 2017 From: timr at probo.com (Tim Roberts) Date: Wed, 15 Nov 2017 14:26:18 -0800 Subject: [python-win32] a question about and error In-Reply-To: <286143289.5272875.1510686039108.JavaMail.apache@nm42.abv.bg> References: <241864665.5272827.1510686000938.JavaMail.apache@nm42.abv.bg> <286143289.5272875.1510686039108.JavaMail.apache@nm42.abv.bg> Message-ID: On Nov 14, 2017, at 11:00 AM, Elitsa Hineva wrote: > > I am trying to run a GIS model for wind fetch calculation but it didn't work. Instead I have received the following note: > > : (-2147467259, 'Unspecified error', None, None) > Failed to execute (fetch_jjr). > > What do i need to do to solve this problem? You need to contact the people who wrote your GIS interface. That is 0x80004005, which is the very generic error E_FAIL. The only people who can help you are the authors of the module. ? Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.peeters at telenet.be Tue Nov 28 12:19:11 2017 From: r.peeters at telenet.be (Richard Peeters) Date: Tue, 28 Nov 2017 18:19:11 +0100 Subject: [python-win32] win32 printer Message-ID: <0C343F2A48CA49A0AE9E862702428498@MEDION> Hello, I'm new to Python and this list. > I installed pypiwin32 , and demo runs without errors.... but nothing is printing. I checked and > nothing in cue > Os is windows7. > BTW is there a simple method to just print text (as lprint in basic)? > tks Richard > code: > ---------------------------------- > #printer > import os, sys > import win32print > printer_name = win32print.GetDefaultPrinter () > print(printer_name) > # > # raw_data could equally be raw PCL/PS read from > # some print-to-file operation > # > 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) > print(hPrinter) > 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) -------------- next part -------------- An HTML attachment was scrubbed... URL: From bhood2 at comcast.net Tue Nov 28 17:08:19 2017 From: bhood2 at comcast.net (Bob Hood) Date: Tue, 28 Nov 2017 15:08:19 -0700 Subject: [python-win32] win32 printer In-Reply-To: <0C343F2A48CA49A0AE9E862702428498@MEDION> References: <0C343F2A48CA49A0AE9E862702428498@MEDION> Message-ID: <2717d1bb-e204-2934-2b6f-cea03421eec5@comcast.net> I don't know specifically what is wrong with your script, but I wanted to chime in here and confirm that it also failed to print anything for me.? No error messages, nothing in the printer queue, and nothing printed. On 11/28/2017 10:19 AM, Richard Peeters wrote: > ?Hello, > I'm new to Python and this list. > > I installed pypiwin32 , > and?demo runs without errors.... but nothing is printing. I checked and > > nothing in cue > > Os is windows7. > > BTW is there a simple method?to just print text (as lprint in basic)? > > > tks Richard > > > code: > > ---------------------------------- > > #printer > > import os, sys > > import win32print > > printer_name = win32print.GetDefaultPrinter () > > print(printer_name) > > # > > # raw_data could equally be raw PCL/PS read from > > #? some print-to-file operation > > # > > 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) > > print(hPrinter) > > 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) > > > _______________________________________________ > python-win32 mailing list > python-win32 at python.org > https://mail.python.org/mailman/listinfo/python-win32 -------------- next part -------------- An HTML attachment was scrubbed... URL: From timr at probo.com Tue Nov 28 17:53:36 2017 From: timr at probo.com (Tim Roberts) Date: Tue, 28 Nov 2017 14:53:36 -0800 Subject: [python-win32] win32 printer In-Reply-To: <0C343F2A48CA49A0AE9E862702428498@MEDION> References: <0C343F2A48CA49A0AE9E862702428498@MEDION> Message-ID: <5e63ea3b-059f-cbbd-7542-2869f98ec385@probo.com> Richard Peeters wrote: > ? > I'm new to Python and this list. > I installed pypiwin32 ,? > and?demo runs without errors.... but nothing is printing. I checked and > nothing in cue > Os is windows7. It depends on your printer.? Your script works fine on my Brother laser printer, but many laser printers don't understand raw text.? If your printer is native Postscript, for instance, it will throw away your raw input, because that's not a Postscript program.? Some of the cheapest laser printers don't have any fonts at all -- they rely on a Windows driver to turn the text into a big bitmap.? Same for the thermal label printers.? What printer are you using? > BTW is there a simple method?to just print text (as lprint in basic)? LPRINT was created when the computing world was a very different place.? It relies on DOS and BIOS calls to send text to the default printer, but that's default in the BIOS sense, meaning a parallel printer hooked up to a parallel port on your computer.? If you don't have a parallel port printer, then LPRINT doesn't work, either.? If you do have a parallel port printer, you can simulate LPRINT by opening the special file "PRN" or "LPT1" and writing to it: ??? lpr = open("PRN","w") ??? lpr.write( "Hello, world!" ) If you're just trying to do logging to a printer, that's tedious but not terribly difficult in Windows.? You just have to maintain your Y position, do TextOut calls, and end the page when it is full.? It's usually way easier to log to a file and print it afterward using something like Notepad. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From timr at probo.com Wed Nov 29 14:24:30 2017 From: timr at probo.com (Tim Roberts) Date: Wed, 29 Nov 2017 11:24:30 -0800 Subject: [python-win32] win32 printer In-Reply-To: <37C00782097F41AE8686C872D39136C1@MEDION> References: <0C343F2A48CA49A0AE9E862702428498@MEDION> <5e63ea3b-059f-cbbd-7542-2869f98ec385@probo.com> <37C00782097F41AE8686C872D39136C1@MEDION> Message-ID: <9bf1b8d0-13d4-aaf3-6564-cb1ec3248018@probo.com> Richard Peeters wrote: > > Tks for reply , but decided to go other way. > The following code prints "Hello world" about an inch down on page at > left margin. > I can change the hoirizontal position from left to right but the > vertical only a few inches down the page. > What are the coordinates to cover a complete page? width = hDC.GetDeviceCaps( win32con.HORZRES ) height = hDC.GetDeviceCaps( win32con.VERTRES ) That returns you the surface size in pixels.? If you plan to work in twips, you'll have to convert it. widthtwips = hDC.GetDeviceCaps(win32con.HORZRES ) * 1440 / hDC.GetDeviceCaps(win32con.LOGPIXELSX) heighttwips = hDC.GetDeviceCaps(win32con.VERTRES ) * 1440 / hDC.GetDeviceCaps(win32con.LOGPIXELSY) By the way, you wrote: ??? INCH = 1440? # twips - 1440 per inch allows fine res Working in twips doesn't increase the resolution in any way.? You can't get any finer than pixels.? It's just a little more convenient.? If you're going to use the "INCH" constant anyway, you could just leave the mapping mode alone, and do ??? INCH = hDC.GetDeviceCaps(win32con.LOGPIXELSX) Now positive Y increases going down, which is a bit more natural. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc.