Did you know you can use Python code to dynamically create Word Documents? For this tutorial, we will be using the Python libraries ‘xlwings’ & ‘docxtpl’ to automate Word. In particular, we will be writing values from Excel to Word by using Python.
Download all the files from the tutorial:
👉 GitHub: https://github.com/Sven-Bo/python-word-automation
𝗖𝗢𝗡𝗡𝗘𝗖𝗧 𝗪𝗜𝗧𝗛 𝗠𝗘:
🌎 Website: https://pythonandvba.com
📝 GitHub: https://github.com/Sven-Bo
⭐ Discord: https://pythonandvba.com/discord
▶️ Subscribe: https://youtube.com/c/CodingIsFun?sub_confirmation=1
🎉 𝗙𝗥𝗘𝗘 𝗘𝘅𝗰𝗲𝗹 𝗔𝗱𝗱-𝗶𝗻 𝘁𝗼 𝗯𝗼𝗼𝘀𝘁 𝘆𝗼𝘂𝗿 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝘃𝗶𝘁𝘆
Get it here: https://pythonandvba.com/mytoolbelt
📚 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗳𝗼𝗿 𝗹𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗩𝗕𝗔 & 𝗣𝘆𝘁𝗵𝗼𝗻
Check out my recommendations: https://pythonandvba.com/resources
☕ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲❓
If you want to support this channel, you can buy me a coffee here:
Is that possible to make a video how to do the same with VBA instead of Python? Thanks
Do you know how can you work with tables dynamically ? Just without knowing the size of rows in the template and only in the excel file. Thank you!
Stupid question most likely, and I did restart the kernel but I get a syntax error from simply writing xlwings quickstart work_automation –standalone. xlwings is fully installed.
I have issues to execute this instruction at the CMD window "xlwings quickstart word-automation –standalone" it tells me that the command "xlwings" is not recognizable as an external command internal and/or external
Instead of integer format, how can I retrieve "source formatting" (###.#, ###>#%, etc.) from the file? in this line of code: context = sht_panel.range("A2").options(dict,expand="table", numbers=int).value
Thanks a lot! Some trial and error as a new Python user, but got there in the end.
Can someone help me, after I install "xlwings" and type " xlwings quickstar <documentname> " it doesn"t work, it says it is not recognized as an intern or external command.
Hey – When I try to install Docxtpl or python-docx it always says pip command not found on my command prompt – do you know why this is this? Thanks
Hi! Many thanks for this wonderful tutorial. Appreciate it. I was able to follow your instructions and design one. It worked. But I have one minor issue. I have a date cell in Excel. I want to populate word with string date for example 1 January, 2022 instead of 1/1/2022. Is there a way out? Thanks once again and have a nice day!😊
Thanks for the video. Quick question though, what if you have a dataframe and you want to generate multiple docs (same format)? Can we loop through the function you created and in each loop take one rows and create one doc?
Is It possible to get the data from Multiple Columns and Multiple Rows ?
for example Col A : Company Name, Col B : Cities, Col C : Revenue and so on.
The Word Document will have 3 paragraph, Each Paragrah will talk about a different company from the above table.
After testing this, I've met some problems:
Traceback (most recent call last):
File "C:UsersxDesktopword_automationexample.py", line 9, in <module>
placeholder_1 = InlineImage(doc, 'Placeholders/Placeholder_1.png', Cm(5))
NameError: name 'InlineImage' is not defined
Is it possible to combine and compare more than ten documents and then merge them into one with amendments being marked in revision mode? Thank you.
Thanks for the great tutorial. I am actually trying to create a very similar system. I have a question regarding the the line: import xlwings as xw and the line: from docxtpl import DocxTemplate. I get the errors Import "xlwings" could not be resolved and Import "docxtpl" could not be resolved. I checked and both libraries are installed. Do you have an idea what might be going wrong in this case? I used exactly the same code as in your video. I am using a mac though.
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:UsersXXXXXXXXXXXdesktopprojectsexceltowordword_automationword_automation.py", line 7, in <module>
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''
Press Ctrl+C to copy this message to the clipboard.
What should I do now? Thanks in advance!
Hello, thank you very much for your very useful video.
I would like to reproduce the same thing but instead of generating all the pdfs at the same time I want the word file to be created on demand.
For example, I want to have a drop-down list that will allow me to select a client and then the code will generate its word file with its information.
The drop-down list can be on Excel or on another interface. Do you have any ideas on how to do this please?
Very well explained, but unfortunately too fast. I can not quite follow the video. Could you please make a detailed basics video on this topic. Thank you. With best regards
Thank you so much for this awesome content. The first part works for me, I am able to render the file based on the template. But when I try to do it using the button on Excel I get this error —————————
Package not found at 'MainTemplate.docx'
I really do not understand as anyone ran into a similar issue?
Thank you for the tutorial!
Thank you so much , i could easily follow along the tutorial and help automate a tidious task my father has to do eveytime.
how can we add border to the paragraph?
Hello, how can I put images from a url? I've tried but can't.
Great video, it helped me a lot. I'm trying to find nice projects for my students. Btw, is there a method for using Google sheets instead of Excel?
much thanks,this touraial gonna help me,like!🥰
Very helpful tutorial, excatly what I needed, thanks a lot. But how can I format numbers with comma separators and round to 2 decimal places?
I am not able to locate the samplecall macro in my excel. How to add it?
Clear and concise. Delivers results and contains plenty of explanation to enable the viewer to adapt to their own project. Thank you for an excellent tutorial.
Thanks so much for the video!!, i was wondering if it is possible to convert all of this files into an .exe so i can run it in different computers that are not using python?
Dude , since i started watch your videos , my desire to learn python has grown.Thanks!
Hi, excellent tutorial! I ran into an issue when assigning the sample call macro to the button – Excel runs into an error, that it can't find Python and to download it from the Microsoft store. The excel template comes with some settings options, but I was unable to resolve the issue. Do you have any recommendations? Thanks.
Hi, this is exactly what I've been looking for. However, I can't seem to get it to run, even though I follow you to each steps. Maybe because I'm using a Mac?
I'd love to if you could give me some suggestions
Here is the error code when I'm running the basic example:
Traceback (most recent call last):
File "example.py", line 16, in <module>
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/template.py", line 317, in render
xml_src = self.build_xml(context, jinja_env)
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/template.py", line 272, in build_xml
xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/template.py", line 227, in render_xml_part
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/docxtpl/template.py", line 220, in render_xml_part
template = Template(src_xml)
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/environment.py", line 1031, in _new_
return env.from_string(source, template_class=cls)
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/environment.py", line 941, in from_string
return cls.from_code(self, self.compile(source), globals, None)
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/environment.py", line 638, in compile
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
File "/Users/Wen_1/opt/anaconda3/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: unexpected char '（' at 4984
(base) [email protected] word_automation %
Very Informative Thanks
well made tutorial sir