0

I use Python 3.6 and Django 1.11. I created a Word document with MailMerge and it was ok. Right now I need to save this document as a pdf document.

import win32com.client as win32
from os import path
word = win32.DispatchEx("Word.Application")
filedoc='c:\\growthtech\\Capturar6.docx'
filepdf='c:\\growthtech\\Capturar6.pdf'
in_file = path.abspath(filedoc)
out_file = path.abspath(filepdf)
doc = word.Documents.Open(in_file, 'rb')
doc.SaveAs(new_file, FileFormat=17)
doc.Close()
word.Quit()

It occurred the error bellow in line "doc = word.Documents.Open(in_file, 'rb')".

>>> doc = word.Documents.Open(in_file, 'rb')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352571, 'Tipo não correspondente.', None, 2)

I appreciate any help.

Patricia
  • 79
  • 1
  • 11
  • Something wrong with arguments of `doc = word.Documents.Open(in_file, 'rb')`... Is `'rb'` really needed? – Evgeny May 17 '18 at 13:16

1 Answers1

0

This should work:

import win32com.client as win32
from os import path
word = win32.DispatchEx("Word.Application")
in_file = path.abspath('c:\\growthtech\\Capturar6.docx')
out_file = path.abspath('c:\\growthtech\\Capturar6.pdf')
# just one argument here
doc = word.Documents.Open(in_file)
# was: 'new_file'
doc.SaveAs(out_file, FileFormat=17)
doc.Close()
word.Quit()
Evgeny
  • 4,173
  • 2
  • 19
  • 39