I am Using the iTunes COM interface on windows 7. The method iTunes.CurrentTrack.AddArtworkFromFile(path) requires path to be of type BSTR.
I understand from some research that BSTR is a C++/Visual Basic data type that means basic string.
Is there any way of converting python ascii strings to BSTR?
code :
import urllib
from BeautifulSoup import BeautifulSoup
import re
import win32com.client
import sys
iTunes = win32c开发者_Python百科om.client.gencache.EnsureDispatch("iTunes.Application")
cTrackName = iTunes.CurrentTrack.Name
cArtist = iTunes.CurrentTrack.Artist
cAlbum = iTunes.CurrentTrack.Album
print cAlbum + cArtist + cTrackName
url = 'http://www.last.fm/music/'+cArtist+'/'+cAlbum
albumPage = urllib.urlopen(url).read()
soup = BeautifulSoup(albumPage)
s = soup.prettify()
z = re.compile('.*<img width="174" src="(.*)" class="art" id="albumCover" itemprop="image" class="albumCover coverMega" />')
p = re.findall(z,s)
print p
urllib.urlretrieve(p[0],'a.png')
a = urllib.quote('file://localhost/C:/Users/ArchAngel/Desktop/a.png')
iTunes.CurrentTrack.AddArtworkFromFile('file://localhost/C:/Users/ArchAngel/Desktop/a.png')
#print binascii.a2b_uu('sadsaffas')
iTunes.CurrentTrack.AddArtworkFromFile(b'C:\Users\ArchAngel\Desktop\a.png')
Error : pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147467259), None)
Since you are using Python 2.x, I believe you simply need to use unicode
rather than str
and win32com
will do the rest. However, I would have expected win32com
to automatically convert str
to unicode
for you.
You should write it like this:
iTunes.CurrentTrack.AddArtworkFromFile(u'file://localhost/C:/Users/ArchAngel/Desktop/a.png')
If this does not work then I think your problem lies elsewhere.
精彩评论