AlJohri / docx2pdf

MIT License
497 stars 94 forks source link

Tries to close other open word files when converting doc to pdf #60

Open bobyfisch opened 2 years ago

bobyfisch commented 2 years ago

I have various open word files in my computer (Windows 10). When launching the following function:

from docx2pdf import convert

def convert_doc_to_pdf(path, pdf_name):

   inputFile = os.path.join(path, 'XXX.docx')
   outputFile = os.path.join(path, pdf_name + '_XXX.pdf')

   print(f"generating pdf {outputFile}")

   if os.path.isfile(outputFile):
       os.remove(outputFile)

   convert(inputFile, outputFile)

The code works well: it opens inputFile, converts it and closes it. I can see all of this because I can see microsoft word openning inputFile and then closing it.

However, when closing InputFile the close command is also trying to close my other open word files, I find this is a bug and should be fixed, as it could result in unwanted behaviour.

AlJohri commented 2 years ago

Does the keep_active flag do what you need?

bobyfisch commented 2 years ago

Hi @AlJohri,

I believe this flag would keep open the file inputFile, this is not exactly to what I was referring - I would like for this file to close but it seems that in doing so it also closes the other open word files I have in my computer.

But thank you for your answer, I will use this flag as leaving InputFile open is less of a problem than closing other word documents that might result in problems.

CDWimmer commented 1 year ago

Came here to make the same comment, it closes all instances of Word, not just the one it briefly runs to do the conversion, keep_active helps but does not perfectly resolve the issue :(

AlJohri commented 1 year ago

can you help me understand how keep_active doesn't resolve the issue? on both windows and macOS, this flag just prevents the entire word application from being closed:

https://github.com/AlJohri/docx2pdf/blob/bc985a13cbc6db455eb9828b5ffec51b4bffb530/docx2pdf/__init__.py#L37-L38

https://github.com/AlJohri/docx2pdf/blob/269757c8b32a22fb525726e34fba98955cec5049/docx2pdf/convert.jxa#L189-L191

regardless of this flag, we still close the input document:

https://github.com/AlJohri/docx2pdf/blob/bc985a13cbc6db455eb9828b5ffec51b4bffb530/docx2pdf/__init__.py#L34

https://github.com/AlJohri/docx2pdf/blob/269757c8b32a22fb525726e34fba98955cec5049/docx2pdf/convert.jxa#L186

but if keep_active is true then I believe the word application should remain open

CDWimmer commented 1 year ago

🤦‍♂️ Ah.

My understanding was that --keep-active would cause the document I converted to remain open afterwards. I had assumed that leaving other instances of Word would be the default action because... well why would you interact with other instances? They have nothing to do with the document I was turning into a PDF and should not be touched, that shouldn't need a special flag imo