not quite random: python
Not Quite Random: snippets of Python.

 


January 2003
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Dec   Feb




Python






Subscribe to "not quite random: python" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

jenett.radio.randomizer - click to visit a random Radio weblog - for information, contact randomizer@coolstop.com

Blogchalk:
Portsmouth, NH, US




 

Friday, January 03, 2003

It looks like Python 2.3 will have some pretty cool stuff.

It looks like Python 2.3 will have some pretty cool stuff.

8:34:10 AM   #  

odbc.py: working with dbiRaw

This post on comp.lang.python describes how to use the dbiRaw object you get back when working with binary (BLOB or IMAGE, depending on your back end) fields. And, for some reason, I seem to be getting back two bytes for every byte inserted. This is annoying.

5:34:15 PM   #  

Tuesday, December 31, 2002

Python module for handling MS Word files

I wrote a simple python module for handling MS Word files. If you've been following along, you know that I've been wrestling with python and Word a bit recently. It turns out that it's easy enough to do, you just have to put together the right information. (Note: requires win32 extensions, Word, and FPropSet.)

7:18:30 AM   #  

Friday, December 27, 2002

Using Python to Extract Title from MS Word Doc

Based on some code from this article, here's how to extract the "Title" property from a Word document in python.

  import win32com.client
  app = win32com.client.Dispatch('Word.Application')
  doc = app.Documents.Add(self.filename)
  self.title = doc.BuiltinDocumentProperties("Title")
  doc.Close(0)
  app.Quit()
3:25:43 PM   #  

Hmm.

Hmm. There has got to be a faster way of getting that info. I'm pretty sure that Explorer isn't launching Word every time it wants to display the properties for a document.

3:47:27 PM   #  

Document Titles Part II: A Better Way

There is: FPropSet. This is about two magnitudes of order faster than the previous solution.

    fps = win32com.client.Dispatch('FilePropertySet.FilePropertySet')
    fps.Pathname = file
    print fps.BuiltInFileProperties().Item("Title")
6:30:51 PM   #  

Do Not Operate Heavy Machinery if Brain is not Working

After spending an hour trying to figure out what misunderstanding of COM was causing my problem, I realized that the file I was trying to test against DID NOT EXIST (typo). I was calling everything correctly, but the information I was sending was wrong... note that setting fps.Pathname to a non-existent file will cause an exception.

6:33:41 PM   #  

Friday, December 20, 2002

upload.py: a script to upload files to FTP server only as-needed

This is a python script that basically implements a crude sort of upstreaming (but it's not scanning constantly, only when you run the script). I wrote this because I got tired of fighting with other software that didn't quite integrate the way I wanted it to. This is a small piece in a set of loosely joined tools that I use to manage my website(s). The script is available under an MIT style license.

7:28:25 PM   #  

Thursday, December 19, 2002

Creating a DSN from Python

This is pretty simple. All I did was to convert the MS KB article from Visual Basic to Python. Note that this won't actually create the .mdb file. That's a little trickier -- I can't find a Python binding for the DLL call and I don't have the need for it at the moment. See init_dsn.py for details.

3:56:05 PM   #  

Wednesday, December 18, 2002

Using the Windows System Tray with wxPython

Check out this code sample for a quick and dirty system-tray demo using wxPython. (Lifted, rearranged, and simplified from the wxPython demo.)

Note that you'll need an icon file for this to work, or you'll have to add back the images.py code from the demo.

11:28:00 AM   #  

Monday, December 16, 2002

MS Word from Python

Note: this assumes you have python, win32all, and MS Word all installed properly.

Running Word from python is incredibly easy.

import win32com.client
# Start the application.
app = win32com.client.Dispatch('Word.Application')
# Open a document.
doc = app.Documents.Add('c:\path\to\document\test.doc')
# Or:
app.ChangeFileOpenDirectory('c:\path\to\document\')
doc = app.Documents.Add('test.doc')
# To save as HTML (filtered; i.e. without most of the MS cruft)
# Note: I don't know if the type-number is portable??
doc.SaveAs('test.html', 10) ## 10 == HTML-Filtered
doc.Close(0) ## 0 == don't save changes?
app.Quit()

This will all run "invisibly". If you want to display the app while doing all of this, set app.Visible = 1 somewhere near the top. If you simply want to fetch the text from the document, use doc.Content.Text. It will return a Unicode string and it's worth noting that those "smart quotes" have no representation in ASCII so you might have to be careful decoding it.

2:21:16 PM   #  


Click here to visit the Radio UserLand website. © Copyright 2003 Brian St. Pierre.
Last update: 1/13/2003; 9:47:03 PM.