0

I am currently using the docx2pdf library in Python to convert Word files to PDF format. However, I have noticed that when I execute the conversion process, it forcibly closes all other Word files that I may have open for editing. Is there a way to prevent this behavior, or are there alternative libraries that I can utilize for this conversion task? I would greatly appreciate any guidance or suggestions.

from docx2pdf import convert
# Specify the input and output file paths
input_file = self.nameProject + '.docx'
current_directory = os.getcwd()
output_file = os.path.join(current_directory, self.nameProject + '.pdf')

# Convert the file and save it to the specified output path
convert(input_file, output_file)
Michael M.
  • 10,486
  • 9
  • 18
  • 34
TariqShah
  • 25
  • 7
  • I see you already posted a Github issue, but one of the other issues there suggests that `keep_active` prevents the library from closing other documents (see https://github.com/AlJohri/docx2pdf/issues/60). – Marijn Jun 08 '23 at 13:54

1 Answers1

0

Having the same problem, I've found that win32com works for me, leaving open Word windows untouched:

from win32com import client
# Specify the input and output file paths

# Convert the file and save it to the specified output path
doc = client.GetObject(input_file)
doc.SaveAs(output_file, FileFormat=17)

This sort of thing works too:

from win32com import client
# Specify the input and output file paths

word = client.gencache.EnsureDispatch('Word.Application')
word.Visible = True
doc = word.Documents.Open(str(input_file))
# make edits etc
doc.SaveAs(str(output_file), FileFormat=17)
doc.Save
doc.Close() # etc

N.B. Converting Path objects to string (I was using pathlib)

BJyou
  • 1
  • 2