Python-docx vs Pywin32

I have recently made a python program which reads  stock trade log data and creates report in word format automatically. When you want to automate reading and writing data to Ms-word, you can use among two python libraries, either python-docx or pywin32.

Before making the decision on which library to chose for my program, I did research on internet. I observed that most of examples on automatic handling of ms-word used python-docx library. There are lot of resources, documentation and tutorial available for python-docx library. But when I searched for documentation for pywin32, there are almost no documentation or tutorials. The examples of use of pywin32 library that I found were more than 5 years old. So I chose python-docx library for my project.

 Anytime your project of word automation is small then python-docx library is one to chose due to its better documentation. However python-docx libary does not implement many feature of word. For example you cannot change cell margins or change header or footer in python-docx in python-docx library.

The report that I was making contained lots of formatting changes, some of which were not possible in python-docx. Hence after working on python-docx library for two weeks I had to shift to pywin32 library.

But once I started working in pywin32. I found it is easy to work with.

You can create a new word document by using following lines:
import win32com.client
wordApp = win32com.client.Dispatch('Word.Application')
doc = wordApp.Documents.Add()

Thereafter you can you use vba methods on doc object. You can get vba methods to use from word vba
reference available here:

https://docs.microsoft.com/en-us/office/vba/api/overview/word/object-model


Then using vba reference I made a program to automatically generate report word report containing
multiple tables.

Comments