This tutorial shows a guide on how to write word file using Python. We know that word is great for documentation. This tutorial also shows how to install docx module when not available in Python in Windows Operating System. These modules are required to write word or docx file using Python.
This word document tool is used in many areas and some of them are given below:
You can create all types of official documents in Microsoft Word.
You can create lecture script by using text, word art, shapes, colors, and images.
You can create a birthday card, invitation card in Microsoft Word by using pre-defined templates or using insert menu and format menus functions.
You can highlight basic and advance knowledge of MS Word as great skill in your resume for the job interview.
You can create notes and assignment on MS-word.
You can create and print a book using MS Word by creating a cover page, content, head and footers, image adjustments, text alignment and text highlighter etc.
You can start your business online and offline. You need to create documents for official works.
You can use Microsoft word to collaborate with your team while working on the same project and document.
What’s more, this software is widely used in many different application fields all over the world and it also applies to data science.
You may like to read:
We have seen various operations on word files using wonderful API – Apache POI in Java technology and it requires few more lines of code have to be written to read from or write to word files.
But to write word file using Python is very easy with a few lines of code. We will create a simple word file here to write word file using Python.
Let’s move on to the example…
Have Python installed in Windows (or Unix)
Pyhton version and Packages
Here I am using Python 3.6.5 version
package – docx
Preparing your workspace
Preparing your workspace is one of the first things that you can do to make sure that you start off well. The first step is to check your working directory.
When you are working in the Python terminal, you need first navigate to the directory, where your file is located and then start up Python, i.e., you have to make sure that your file is located in the directory where you want to work from.
Check Required Modules
Check for modules docx in Python terminal. Type the command as shown below to check docx package. If you do not get any error message then the module exists otherwise you have to install the non-existence module.
>> import docx
If you do not have docx module available then please go through tutorial A guide to read word file using Python(https://www.roytuts.com/read-word-file-using-python/) to install docx module in Windows Operating System.
Now let’s move on to the example write word file using Python.
In the below image you see I have opened a cmd prompt and navigated to the directory where I have put the word file that has to be written.
Now create a Python script write_word.py under the C:\py_scripts for writing to the word file. Here py is extension of the Python file.
In the below Python script notice how we imported docx module.
The below Python script shows how to write word file using Python.
from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) p = document.add_paragraph('A plain paragraph having some ') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True document.add_heading('Heading, level 1', level=1) document.add_paragraph('Intense quote', style='Intense Quote') document.add_paragraph( 'first item in unordered list', style='List Bullet' ) document.add_paragraph( 'first item in ordered list', style='List Number' ) document.add_picture('C:\\Users\\Pictures\\application.jpg', width=Inches(1.25)) document.save('simple.docx')
When you execute the above Python script, then you should see the following word file created under the directory C:\py_scripts.
Here is the sample file simple.
Hope you understood how to write word file using Python.
Thanks for reading.