create account

Automating Microsoft Word Documents With Python-DOCX by geekgirl

View this thread on: hive.blogpeakd.comecency.com
· @geekgirl ·
$83.76
Automating Microsoft Word Documents With Python-DOCX
<center>![docx.png](https://images.hive.blog/DQmNW7uUyU3mZ2vfvWCiikNL6N63x5cCxE2CEemuwcvtc6g/docx.png)</center>

Automating simple repetitive tasks is just awesome. It saves a lot of time and time is one of the most precious things we have. We all perform different kinds of repetitive work either in professional or personal lives. Thanks to technology and programming tools, we are able to let the computers do the heavy lifting. I have written in the past about automating stocks and crypto market data, extracting data from pdf files, building excel spreadsheets, etc. Until recently, I haven't tried and didn't have a need for automating Microsoft Word documents. I was sure there are easy to use solutions available for top programming languages, especially for python.  When I was received a request to automate MS Word forms, I didn't have to think twice and was glad for an opportunity to experiment with automating documents.

[Python-docx](https://python-docx.readthedocs.io/en/latest/) is one of the python libraries that allows us to create and edit Microsoft Word files. It's documentation let's us get started and experiment with **docx** super quick. However, it lacks more detailed explanation of solving more complex problems. Python community is big, and there is plenty of resources available that help finding the right solutions.

The script/app I was writing had a very simple goal. To create a template of an existing document, let the user enter some of the data within an app and create a final document with proper naming. Since most of the data entered would be either in unchanging list, and options were limited, and only some of the items would have to change on daily basis, it did make sense to automate this process. This would save hours and decrease the time spent to seconds. It is interesting how many companies and organizations have so much bureaucracy involved and don't offer more efficient solutions to manage such processes. 

To get started with python-docx is super simple. The documentation provides the following template code, which was copying and used in many tutorials on library. I too will share the code, just to demonstrate how easy it is to automate Microsoft Word documents.

```

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('monty-truth.png', width=Inches(1.25))

records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc

document.add_page_break()

document.save('demo.docx')

```

The code self explanatory and once the document is created, by viewing the document we can see which line of code is create what paragraphs or parts of the document. I normally prefer to share my own code in posts like this. However, since the script I created had to do with a specific task and had to be run as app, it would be able to show how easy it is to use docx. The sample code above creates a documents and starts adding part of the document like the heading, paragraph, parts of the paragraph, unordered and ordered lists, tables, images, and applying styles.

When it comes to styles, fonts, colors, and layouts ***docx*** has multiple ways of achieving them. It is not clear right away what the standard or best practices are. In my situation, I had to experiment with different solutions to figure out a better way. While some actions are self explanatory and answers are available in the documentation, more specific situations are not easy to find solutions for. For example, in my document I had to create borders around some paragraphs. I wasn't able to find a solution in the documentation. I found the solution elsewhere, where someone who had a similar issues and shared their solution. But it didn't involve using simple methods and properties. Rather it involved lower level usage of document formatting. That part perhaps would require more studying and experimenting to take advantage of. But it also shows how powers this tool is, and very complex document task solutions can be found with enough time and effort.

I prefer integrating python scripts with Streamlit. This way scripts can be turned into apps and can easily be used by non-programmers.  Streamlit apps are great to share automating solutions with teams, colleagues, friends, and clients. It gets the job done and doesn't require much of web development skills. 

Have you used **python-docx** or other automating tools? Feel free to share your thoughts, experiences, and tools in the comments.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 595 others
👎  , , , , ,
properties (23)
authorgeekgirl
permlinkautomating-microsoft-word-documents-with-python-docx
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"image":["https://images.hive.blog/DQmNW7uUyU3mZ2vfvWCiikNL6N63x5cCxE2CEemuwcvtc6g/docx.png"],"links":["https://python-docx.readthedocs.io/en/latest/"],"app":"hiveblog/0.1","format":"markdown"}
created2023-01-08 05:18:00
last_update2023-01-08 05:18:00
depth0
children17
last_payout2023-01-15 05:18:00
cashout_time1969-12-31 23:59:59
total_payout_value41.928 HBD
curator_payout_value41.829 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,315
author_reputation1,215,230,273,660,381
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,702,978
net_rshares196,121,990,899,586
author_curate_reward""
vote details (665)
@awah ·
$0.19
This is really useful.  I haven't seen this library before but am sure I can find a use for it now.  Thanks for sharing.
👍  , , , , , ,
properties (23)
authorawah
permlinkre-geekgirl-202318t1759139z
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"app":"ecency/3.0.30-vision","format":"markdown+html"}
created2023-01-08 17:59:15
last_update2023-01-08 17:59:15
depth1
children0
last_payout2023-01-15 17:59:15
cashout_time1969-12-31 23:59:59
total_payout_value0.096 HBD
curator_payout_value0.096 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length120
author_reputation85,988,161,583,759
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,715,640
net_rshares431,092,904,934
author_curate_reward""
vote details (7)
@chinito ·
automation is really great.

Yes PLS! 
properties (22)
authorchinito
permlinkre-geekgirl-ro6nlo
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2022.12.1"}
created2023-01-08 20:00:57
last_update2023-01-08 20:00:57
depth1
children0
last_payout2023-01-15 20:00:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length38
author_reputation150,490,460,685,947
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,718,571
net_rshares0
@curation-cartel ·
![1UP-PIZZA.png](https://files.peakd.com/file/peakd-hive/curation-cartel/23xediR4hotaNsS5pUJrmYVg3YGeTLpui41uCij2jhUDZ4uFT84zoGJf8a8VnfELXLJgt.png) |  <div class="phishy"><u><h4>You have received a __1UP__ from @gwajnberg!</h4></u></div> The @oneup-cartel will soon upvote you with:<hr> __@stem-curator, @vyb-curator, @pob-curator__ <hr>_And they will bring !PIZZA 🍕._
-|-

<sup>[Learn more](https://peakd.com/hive-102223/@flauwy/the-curation-cartel-1up-trigger-smart-voting-mana-and-high-delegation-returns-for-14-different-tribes) about our delegation service to earn daily rewards. Join the Cartel on [Discord](https://discord.gg/mvtAneE3Ca).</sup>
properties (22)
authorcuration-cartel
permlinkre-automating-microsoft-word-documents-with-python-docx-20230108t163101z
categoryhive-163521
json_metadata"{"app": "beem/0.24.26"}"
created2023-01-08 16:31:03
last_update2023-01-08 16:31:03
depth1
children0
last_payout2023-01-15 16:31:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length651
author_reputation1,123,281,846,602
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,713,850
net_rshares0
@daniky ·
This is awesome and a welcome development. Thank you for this update.
properties (22)
authordaniky
permlinkro7mb5
categoryhive-163521
json_metadata{"app":"hiveblog/0.1"}
created2023-01-09 08:30:42
last_update2023-01-09 08:30:42
depth1
children0
last_payout2023-01-16 08:30:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length69
author_reputation91,968,925,185,340
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,731,407
net_rshares0
@djbravo ·
The biggest advantage of technology is that it is saving us time. And if we use this time in the right place, we can achieve a lot of success. so in Pakistan is getting a lot of use now every small and big businessman has set this computer in the shop. Its biggest advantage is that you know within minutes what is short and what is high in your store. We will also use this tool as it is an automatic tool and take advantage of it. Thanks for sharing this new invention with us.
properties (22)
authordjbravo
permlinkro6god
categoryhive-163521
json_metadata{"app":"hiveblog/0.1"}
created2023-01-08 17:30:36
last_update2023-01-08 17:30:36
depth1
children0
last_payout2023-01-15 17:30:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length479
author_reputation194,585,826,027,793
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,715,013
net_rshares0
@etselec23 ·
I didn't know about Python.. but after reading this, it caught my interest. I need to learn more about it. 
properties (22)
authoretselec23
permlinkre-geekgirl-202318t161915374z
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"app":"ecency/3.0.30-vision","format":"markdown+html"}
created2023-01-08 08:19:18
last_update2023-01-08 08:19:18
depth1
children0
last_payout2023-01-15 08:19:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length107
author_reputation10,035,684,447,147
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,705,072
net_rshares0
@gwajnberg ·
Lol word edition with python
!1UP
properties (22)
authorgwajnberg
permlinkre-geekgirl-202318t92431812z
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-08 16:24:42
last_update2023-01-08 16:24:42
depth1
children0
last_payout2023-01-15 16:24:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length33
author_reputation151,602,336,714,362
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,713,719
net_rshares0
@jloberiza ·
I'm not a Pyhton interesting buy this piece of code is quite interesting
👍  , , ,
properties (23)
authorjloberiza
permlinkre-geekgirl-202318t13558527z
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"app":"ecency/3.0.30-vision","format":"markdown+html"}
created2023-01-08 05:55:18
last_update2023-01-08 05:55:18
depth1
children0
last_payout2023-01-15 05:55:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length72
author_reputation17,647,487,593,745
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,703,420
net_rshares751,723,145
author_curate_reward""
vote details (4)
@n1t0 ·
Excellent! I would like to learn some Phyton!
properties (22)
authorn1t0
permlinkre-geekgirl-robv49
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.1.1"}
created2023-01-11 15:28:54
last_update2023-01-11 15:28:54
depth1
children0
last_payout2023-01-18 15:28:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length45
author_reputation1,044,943,615,577
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,794,562
net_rshares0
@olympicdragon ·
$0.10
I heard python is one of the easiest to learn. There is many apps that can assist you to coding, It's so interesting !
👍  , , , , ,
properties (23)
authorolympicdragon
permlinkre-geekgirl-202318t21497197z
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-08 13:49:09
last_update2023-01-08 13:49:09
depth1
children0
last_payout2023-01-15 13:49:09
cashout_time1969-12-31 23:59:59
total_payout_value0.050 HBD
curator_payout_value0.050 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length118
author_reputation33,642,171,440,484
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,710,243
net_rshares232,124,851,556
author_curate_reward""
vote details (6)
@pizzabot ·
<center>🍕 PIZZA !


I gifted <strong>$PIZZA</strong> slices here:
@curation-cartel<sub>(4/20)</sub> tipped @geekgirl (x1)


<sub>Send $PIZZA tips in <a href="https://discord.gg/hivepizza">Discord</a> via tip.cc!</sub></center>
properties (22)
authorpizzabot
permlinkre-automating-microsoft-word-documents-with-python-docx-20230108t163133z
categoryhive-163521
json_metadata"{"app": "beem/0.24.19"}"
created2023-01-08 16:31:33
last_update2023-01-08 16:31:33
depth1
children0
last_payout2023-01-15 16:31:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length226
author_reputation6,176,617,389,928
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,713,864
net_rshares0
@poshtoken · (edited)
https://twitter.com/1460818780980973570/status/1611965031167496192
https://twitter.com/27706436/status/1612036569971691520
<sub> The rewards earned on this comment will go directly to the people( @hivehotbot, @seckorama ) sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.</sub>
properties (22)
authorposhtoken
permlinkre-geekgirl-automating-microsoft-word-documents-with-python-do41343
categoryhive-163521
json_metadata"{"app":"Poshtoken 0.0.1","payoutToUser":["hivehotbot","seckorama"]}"
created2023-01-08 05:58:42
last_update2023-01-08 10:42:57
depth1
children0
last_payout2023-01-15 05:58:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length337
author_reputation3,943,098,116,045,053
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries
0.
accountreward.app
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id119,703,453
net_rshares0
@redditposh ·
https://reddit.com/r/hivenetwork/comments/106g9kl/automating_microsoft_word_documents_with/
<sub> This post was shared on Reddit. Register with @poshtoken, sign up at https://hiveposh.com.</sub>
properties (22)
authorredditposh
permlinkre-geekgirl-automating-microsoft-word-documents-with-python-do41343
categoryhive-163521
json_metadata"{"app":"Poshtoken 0.0.2","payoutToUser":[]}"
created2023-01-08 10:41:24
last_update2023-01-08 10:41:24
depth1
children0
last_payout2023-01-15 10:41:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length195
author_reputation361,187,588,989,728
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries
0.
accountreward.app
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id119,707,160
net_rshares0
@slobberchops ·
$0.30
Very cool, and good for populating templates with individual data. I do have to write up documents which is tedious as hell.., with my new found skills in Python I will likely use this library in my new role if they insist on documents (which nobody ever reads).
👍  , , , , ,
👎  
properties (23)
authorslobberchops
permlinkre-geekgirl-ro5v44
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2022.12.1"}
created2023-01-08 09:45:39
last_update2023-01-08 09:45:39
depth1
children0
last_payout2023-01-15 09:45:39
cashout_time1969-12-31 23:59:59
total_payout_value0.150 HBD
curator_payout_value0.150 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length262
author_reputation2,004,902,424,541,389
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,706,313
net_rshares700,529,496,764
author_curate_reward""
vote details (7)
@stemsocial ·
re-geekgirl-automating-microsoft-word-documents-with-python-docx-20230108t183152853z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-geekgirl-automating-microsoft-word-documents-with-python-docx-20230108t183152853z
categoryhive-163521
json_metadata{"app":"STEMsocial"}
created2023-01-08 18:31:51
last_update2023-01-08 18:31:51
depth1
children0
last_payout2023-01-15 18:31:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,463,635,119,853
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,716,418
net_rshares0
@torran ·
!PGM
!PIZZA
properties (22)
authortorran
permlinkre-geekgirl-ro8noz
categoryhive-163521
json_metadata{"tags":["hive-163521"],"app":"peakd/2023.1.1"}
created2023-01-09 21:53:51
last_update2023-01-09 21:53:51
depth1
children0
last_payout2023-01-16 21:53:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11
author_reputation7,960,046,992,601
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,749,066
net_rshares0
@videoaddiction ·
$0.10
My company have been trying to turn some repetetive work processes into robotic and they are very succesful till now and they remove some burdens/lots of work from workers. They may have used python for this transition.
👍  , , , , ,
properties (23)
authorvideoaddiction
permlinkre-geekgirl-202318t12823106z
categoryhive-163521
json_metadata{"tags":["hive-163521","python","dev","coding","stem","automating","vyb","proofofbrain"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2023-01-08 09:08:24
last_update2023-01-08 09:08:24
depth1
children0
last_payout2023-01-15 09:08:24
cashout_time1969-12-31 23:59:59
total_payout_value0.050 HBD
curator_payout_value0.049 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length219
author_reputation125,092,955,376,730
root_title"Automating Microsoft Word Documents With Python-DOCX"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,705,741
net_rshares232,972,109,203
author_curate_reward""
vote details (6)