简体   繁体   中英

Converting a VBScript to Python

I have the following vbscript running on a Windows machine:

db = "\\networklocation\Database\employeeID.mdb"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set mstream = CreateObject("ADODB.Stream")
'ADODB.Stream is need to transfer the BLOB Data as Binary

mstream.Type = 1 '' 1 is to: adTypeBinary 
'for info on Stream: http://www.w3schools.com/asp/ado_ref_stream.asp

cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; " & "Data Source =" & db

strSQL = "SELECT IDNumber,photo FROM employeeID WHERE IDNumber like 'A00______' and photo is not null" 
'% for 0 or more characters, _ to replace a single character

rs.Open strSQL, cn
DO WHILE not rs.eof
mstream.Open
mstream.Write rs("photo")
mstream.SaveToFile "C:\images\all\" & rs("IDNumber") & ".jpg", 2 'adSaveCreateOveWrite
mstream.close
rs.movenext
Loop

It does exactly what I need it to do, but after getting the pictures, I need to send them to a Linux machine, which I use winscp to send.

I would like to run the script from the Linux server, taking the need for winscp and the windows machine (I don't like running stuff from multiple locations) out of the equation.

I recently discovered that Python can connect to an Access database using pyodbc.

I believe I understand how to connect to the access database, but not sure about how to extract binary data out of one column and label them with a different column in Python. (Extracting the binary data was the biggest hurdle I had when creating the VBScript)

UPDATE: Looks like I can close this idea for now, I didn't realize that all the ODBC Drivers on Linux cost money. searching for other methods now, like jackness which is a direct java connection to the access

You can write the column data into a file in binary mode.

Eg Assuming the image is in first column...


data = cursor.execute(selectstmt).fetchone()
with open('img.jpg', 'wb') as f:
    f.write(data[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM