我正在使用XLWINGS向Excel添加一些功能。我希望能够使用具有特定编解码器的特定音频播放器在单击excel单元格中的超链接时播放音频文件(而不是在将超链接公式写入其单元格时)。
xlwings UDF是:
@xw.func(volatile=False)
def play_audio(audiofilepath):
'''
Use the audioplayer defined at the top of the module as a
path to an executable to
open the audiofile. The audioplayer auto-closes when the
reproduction of the audio file is complete as the timeout
parameter is equal to the calculated duration of the file
'''
#Get file length in seconds
with contextlib.closing(wave.open(audiofilepath,'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
length = frames / float(rate)
'''
Use the defined audioplayer to play the file and
close out the app after the files duration
'''
try:
subprocess.run([audioplayer,audiofilepath], timeout = length)
except:
pass
XLWINGS用来写excel的超链接的代码是:
write_cell.value = '=HYPERLINK(play_audio("{}"),"OK")'.format(fullpathandfilename)
我也试过了:
write_cell.add_hyperlink(address = '=play_audio("{}")'.format(fullpathandfilename),text_to_display ="OK",screen_tip=fullpathandfilename)
编写的excel超链接如下所示:
=HYPERLINK(play_audio("path\to\audio.wav"),"PLAY")
这有效,但我有不受欢迎的行为: