开发此脚本时,测试文件将保存到该位置。实际文件不会传递给Document.open()函数。
我在计算机上尝试过此操作。由于它与网络交互,因此可能无法正常工作。
print('-------------------------------------Setting Variables-----------------------------------------')
oldlocation_forms = 'Old file location'
newlocation = 'location on my pc that is shared on network'
newlocation_sub = 'nested file that is shared on network'
form_x = 'file that needs to be converted*.doc'
print('form_x =' + form_)
os.chdir(oldlocation_forms)
for file_x in glob.glob(form_x):
path_x_convert = oldlocation_forms + '/' + file_x
print('file_x =' + file_)
print('-------------------------------------Local variables---------------------------- ')
baseDir = oldlocation_forms
word = win32com.client.Dispatch("Word.application")
print('-------------------------------------Doc Convert-------------------------------- ')
for dir_path, dirs, files in os.walk(baseDir):
for file_name in files:
file_path = os.path.join(dir_path, '//'+file_x)
file_name, file_extension = os.path.splitext(file_path)
print('**************--------------------------------------------------------------------------------------------------------------------looking')
print(' file_path = ' + file_path)
print(' file_name = '+ file_name)
print(' file_extension = ' + file_extension)
if file_extension.lower() == '.doc': #
docx_file = '{0}{1}'.format(file_path, 'x')
print(' docx_file = ' + docx_file)
try:
word_doc = word.Documents.Open(docx_file)
save_location_ = newlocation + '/' + file_x + 'x'
print(' save_location_x = ' + save_location_x)
word_doc.SaveAs2(save_location_x, FileFormat = 16)
print('**************Moved')
except Exception:
print('Failed to Convert: {0}'.format(docx_file))
word.Quit()