我正在使用带有IMAPI2刻录CD / DVD的模块在Python上编写程序。 我使用了以下示例:codeplex和comtypes doc。
import comtypes.client as cc
class MyProgram(object):
def main(self):
# index of recording drive
index = 0
# files to transfer to disc
path = r'D:\Example'
# DiscMaster2 object to connect to optical drives
discMaster = cc.CreateObject('IMAPI2.MsftDiscMaster2')
# cc.ShowEvents(discMaster) ### uncomment for print events
# DiscRecorder object for specified burning device
recorder = cc.CreateObject('IMAPI2.MsftDiscRecorder2')
uniqueId = discMaster.Item(index)
recorder.InitializeDiscRecorder(uniqueId)
# create an image stream for a specified directory
# create a new file system image and retrieve root directory
_fsi = cc.CreateObject('IMAPI2FS.MsftFileSystemImage')
_dir = _fsi.Root
_fsi.VolumeName = 'TestName'
# create the new disc format and set the recorder
dataWriter = cc.CreateObject('IMAPI2.MsftDiscFormat2Data')
# cc.ShowEvents(dataWriter) ### uncomment for print events
dataWriter.Recorder = recorder
dataWriter.ClientName = 'MyClient'
dataWriter.ForceOverwrite = True
_fsi.ChooseImageDefaults(recorder)
# get events
sink = Sink()
_events = cc.GetEvents(dataWriter, sink)
# print(_events) ### <comtypes.client._events._AdviseConnection object at 0x...>
# add the directory and its content to the file system
_dir.AddTree(path, False)
# create an image from the file system
_result = _fsi.CreateResultImage()
_stream = _result.ImageStream
# write disc using the specified recorder
print('Writing content to disc...')
dataWriter.Write(_stream)
print('Finished writing content')
使用名为'dataWriter'事件的方法创建自定义类:
class Sink(object):
def DDiscFormat2DataEvents_Update(self, this, progress):
strTimeStatus = 'Time: ' + progress.ElapsedTime + ' / ' + progress.TotalTime
print(strTimeStatus)
运行此脚本:
prog = MyProgram()
prog.main()
我可以写CD / DVD,但无法监控进度,我无法理解,为什么。
有没有人有任何想法?
非常感谢。