Programming Python.COM Objects in Django

2 28
Avatar for BigDocIriya
3 years ago

Abstract: This article provides an introduction to Python programming using the Django framework. Creation of a virtual environment and installation of libraries. An example of working with ActivePython and COM objects.

Python is currently one of the most demanded programming languages. Python is easy to use and is also an object oriented programming language.

One of the best tools for developing web services in the Python programming language is the Django framework. The Django library also has ready-made templates for registration, authorization, the administrator part is created automatically, which saves a sufficient amount of time.

Pros of using Django:

  • Availability of the administrative part;

  • Selection of ready-made templates;

  • Extensive documentation;

  • Sufficiently high speed of work.

COM objects are a standard defined by Microsoft. Thanks to COM objects, it is possible to open word, excel, power point, etc.

COM objects are included in the ActivePython distribution.

With the help of Django, it is possible to implement a web service with a call to Word or Excel, write existing data, save. To get started, you need to install a virtual environment (virtualenv), direct installation of the Django framework.

Creating a virtual environment in Python39:

C:\myproject>C:\Python39\Scripts\virtualenv.exe venv

The first part "C:\myproject>" - is the current directory.

The second part "C:\Python39\Scripts\virtualenv.exe" - is the path where the virtual environment is installed (the virtual environment is installed: C:\Python39\Scripts\pip install virtualenv).

The third part of venv is the name of the virtual environment.

After installing the Django libraries using pip install Django

Creating a function in views.py in Figure 1.

Fig. 1. Function in views.py

To work with COM objects, you need to use win32com.client.

In this function import win32com.client as client.

word = client.Dispatch ("Word.Application") - work with word, if you need to work with other office applications instead of the word word set the name of the application (for example Excel.Application).

doc = word.Documents.Open (“C:\\myproject\\Test.rtf”) - opening word, in this case a rtf (Rich Text format) file.

The installed rtf file has a MergeField field, you must use the macros built into Microsoft Office.

After opening the word of the document, it finds the first macro and replaces this macro with the number 123. Saves the changes and exits with word, you need to release the word from their processes.

This example covered a simple way to call and write COM objects in Python in Django. In a more in - depth study, it is possible to automate data entry in word, excel, etc. on the developed site.

One of the biggest drawbacks of using win32com is the inability to deploy on a linux server. Which is a big problem since win32com was developed specifically for Windows.

Sponsors of BigDocIriya
empty
empty
empty

5
$ 0.01
$ 0.01 from @Infinity
Sponsors of BigDocIriya
empty
empty
empty
Avatar for BigDocIriya
3 years ago

Comments

Python is a delight. The way of doing things simple is impressive. In c ++ step I work with some methods while phyton solves it with a line of code. Nice work.

$ 0.00
3 years ago

Thanks! I also started learning C ++, and after getting to know Python I fell in love with it because of its pleasant simplicity)

$ 0.01
3 years ago