1

Does anyone have any experience in opening many Word docs and saving them to PDF, but when they're on a shared server and it may be possible that someone else is editing them at the moment, but you still want to go through and save the PDF?

I have some code using comtypes as follows, which works for files that aren't being edited by another user on the shared drive, but will fail if any of the docs are currently open by a coworker, as they open as read only and i cannot save.

word=comtypes.client.CreateObject(Word.Application)
word.Visible = 1
input_file_path = input_file_path
doc = word.Documents.Open(input_file_path)
file_name = os.path.splitext(input_file_name)[0]
output_file_path = output_file_path
doc.SaveAs(output_file_path,32)
doc.Close()
word.Quit()

1 Answers1

0

open the word document as a data file, open a temporary word document as an output file, read data of BUFSIZE chunks in a loop, write the data to the output file, close files when complete. Basically, do a binary copy of the word file to a temporary file, then PDF-ize your resultant docx temporary file and remove it.

It's an extra step and will require additional disk space for the temporary file, but if you aren't constrained it would certainly be safer than worrying about open files.

This post: Python writing binary files, bytes offers some ideas on copying making copies of binary files.

Fubar
  • 251
  • 2
  • 8