Python code to dynamically create Word documents based on 1 template file | how to use 'docxtpl'

preview_player
Показать описание
#Automation #Python #CSV #DOCX
Automating repetitive tasks is one of my favourite (work) things to do.
Boss: "Hello, we need you to upgrade 100 routers, you'll need to do a change for each one"
Me : *Hmmmm (100 word documents, with only 3 or 4 things different in each one). I'll pretend to do them all manually, and then automate the f*** out of it.

So this video is here for you all to enjoy, and copy if you like. It's work in progress, let me know how to improve it, I'm thinking I'll add a random time interval between the creation of each file, and also read the source data from a csv with 100 router names and ip addresses in.

use : pip3 install docxtpl and create a template file, where the {{stuff}} stuff in between the braces is your variable. Easier to see from video really.

--------------------------------------------------------------------
If you need a good proxy, I can recommend this one 👍👍👍
Coupon Code: DRPI10
-------------------------------------------------------------------

#word #python #docx #automation

Check out the Minimalist online python IDE :

Рекомендации по теме
Комментарии
Автор

Updated version :



'''
Dynamically generate word documents using data from a CSV - with 1 template file.
'''
# pip install python-docx
# pip install docxtpl <- Better for making new files from a template
import random
import time
import csv
import pandas as pd
from docxtpl import DocxTemplate

# Source CSV - column names that must match the *** that are {{***}} inside "template.docx"
csvfn = "cnw.csv"

def mkw(n):
tpl = DocxTemplate("template.docx") # In same directory
df = pd.read_csv(csvfn)
df_to_doct = df.to_dict()
print(df)
x = df.to_dict(orient='records')
context = x

tpl.render(context[n])
tpl.save("%s.docx" %str(n+1))
# Wait a random time - increase to (60, 180) for real production run.
wait = time.sleep(random.randint(1, 2))

### Main ###

df2 = len(pd.read_csv(csvfn))
print ("There will be ", df2, "files")

for i in range(0, df2):
print("Making file: ", f"{i}, ", "..Please Wait...")
mkw(i)
print("Done! - Now check your files")

python
Автор

I just spent an age searching around for how this worked, nearly going to the documentation. Thanks for the save

michaelhearmon
Автор

Wow, that was so much easier than I thought! I can imagine lots of applications for this.

Akerfeldtfan
Автор

Hey Dr Pi, this was very helpful! One of the rare occasion I found the scrip I was looking for without any hassle.
Is there a way to insert a picture instead of text?

chiyto
Автор

this was very helpful and I never read the documentation too. lol

ipgsageuniversity
Автор

Hi Dr Pi, this is very helpful! I am wondering if it is possible to do multiple renders of the same document? I would like first render to merge subdocs based on inputs, and then 2nd render to render all jinja tagged variables(e.g., {{ name }} ) in the template and subdocs.

erichafner
Автор

Thanks Dr. Pi
Which python version are you using?
I am having troubles with 3.8

alvifa
Автор

Basically mailmerge for Python. Very handy!

ethanreynolds
Автор

Wow great. Can you replace context values with entrybox from tkinter?

bentsionben-david
Автор

Can we use this to auto populate bookmarks and table of content created via python-docx, without having to open the generated docx file?

rainygal
Автор

Hi Dr Pi, thank you for your contribution. Hey i need to create a .doc template to create different reports that will extract tha data from different excel file with the same structure, do you have any other video that might be useful for me. Thanks again.

Galvis
Автор

Thanks Dr. Pi
Quick question, i've been struggling for days to find a way to import 40-50 rows with 12 columns from excel to a word table template.
Can you help me?

VikDeeJayMusic
Автор

Hello, Thank you for the tutorial, I have a question
How to save generated Docx using Tkinter GUI File Dialog?

abiwigames
Автор

awesome... Thanks a lot, saved lot of my time.

Kinston
Автор

hi i wonder how to make file names by ids row if there would be for example company names, i mean not only n+1 in tpl.save.

jensvonhonksen
Автор

hi need ur help
I want to format 11 page docx file using a sample need complex coding skills can u please help me 😊😊🙌

sumeetpathania
Автор

how can i read my template in wordfile ? thanks

joaofolgado
visit shbcf.ru