Open DOC File

Information, tips and instructions

Create a DOC file with Python

DOC: This type of extension is mainly related to Microsoft Word, it was created and published by the Microsoft Corporation in its versions of Word between 1997 and 2003.

One of the advantages of using Microsoft Word DOC files is that the applications which can edit them are practically available everywhere.

If you need to do some last minute editing, it is very easy to find a device with an office suite, even mobile devices.

Python: is an interpreted programming language which philosophy emphasizes the intelligibility of its code and the intuitive way in which its statements are presented. It is a multiparadigm programming language, since it partially supports:

  • Object-oriented programming.
  • Imperative programming.
  • Functional programming

It is an interpreted, dynamic and multiplatform language as it is supported by Windows, MAC and Linux. Assuming that you already have the Python language installed, then we will proceed to prepare everything to create a DOC with Python.

Creating a file in Python is very simple, you can do it using the following code:

>>> import os

>>> file = open ("/home/user/file.doc", "w");

>>> file.write ("First testing Line" + os.linesep);

>>> file.write ("Second testing Line");

>>> file.close ()

Code

The 'open' function accepts the path of the file that will be created and the second argument is the mode in which the file will be opened, once created, where w indicates that it is for writing and if you change it to r then it will be for read only.

The variable 'op.linesep' will allow you to obtain the necessary characters to create the line break according to the operating system where you are executing this routine.

If you run the code you will get a result like the following:

FileDoc

A small DOC file is created with the two lines that you wrote with the write function. Open the DOC file and you can see the lines:

 Output


Of course, in your code you must change the path of the file you are going to create and make sure you have write permissions to the destination folder, and then you must find the created file.

In this article, Python3 was used from the command terminal.