0
    import win32com.client
    word = win32com.client.Dispatch("Word.Application")
    word.visible = 1
    pdfdoc = 'NewDoc.pdf'
    todocx = 'NewDoc.docx'
    wb1 = word.Documents.Open(pdfdoc)
    wb1.SaveAs(todocx, FileFormat=16)  # file format for docx
    wb1.Close()
    word.Quit()

I followed this python code from the given link. https://stackoverflow.com/a/63255120/7940894 posted by https://stackoverflow.com/users/10655543/omkar

    import win32com.client
    import pythoncom
    import sys
    word = win32com.client.gencache.EnsureDispatch("Word.Application")
    wb1 = word.Documents.Open(sys.path[0]+'\\'+pdf, pythoncom.CoInitialize())
    import time
    time.sleep(3)
    wb1.SaveAs(doc, FileFormat=16, Encoding='utf-8')
    wb1.close()
    word.Quit()

this is my code...

and this is the error.

---------------------------------------------------------------------------
com_error                                 Traceback (most recent call last)
Input In [44], in <cell line: 1>()
----> 1 wb1.SaveAs(doc, FileFormat=16, Encoding='utf-8')
      2 wb1.close()
      3 word.Quit()
    

File ~\AppData\Local\Temp\gen_py\3.9\00020905-0000-0000-C000-000000000046x0x8x5\_Document.py:438, in _Document.SaveAs(self, FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks)
    434 def SaveAs(self, FileName=defaultNamedOptArg, FileFormat=defaultNamedOptArg, LockComments=defaultNamedOptArg, Password=defaultNamedOptArg
    435         , AddToRecentFiles=defaultNamedOptArg, WritePassword=defaultNamedOptArg, ReadOnlyRecommended=defaultNamedOptArg, EmbedTrueTypeFonts=defaultNamedOptArg, SaveNativePictureFormat=defaultNamedOptArg
    436         , SaveFormsData=defaultNamedOptArg, SaveAsAOCELetter=defaultNamedOptArg, Encoding=defaultNamedOptArg, InsertLineBreaks=defaultNamedOptArg, AllowSubstitutions=defaultNamedOptArg
    437         , LineEnding=defaultNamedOptArg, AddBiDiMarks=defaultNamedOptArg):
--> 438     return self._oleobj_.InvokeTypes(376, LCID, 1, (24, 0), ((16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17), (16396, 17)),FileName
    439         , FileFormat, LockComments, Password, AddToRecentFiles, WritePassword
    440         , ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter
    441         , Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks
    442         )

com_error: (-2147418111, 'Call was rejected by callee.', None, None)```
  • Oddly, the Word Document object does not have a SaveAs method: https://learn.microsoft.com/en-us/office/vba/api/word.saveas2 In the `SaveAs2` function that `Encoding` is not a string but a constant: msoEncodingUTF8 = 65001. Go to your %AppData%\Local\Temp\gen_py folder and delete everything. Remove the pythoncom stuff, you don't need that. – DS_London Oct 10 '22 at 18:18

0 Answers0