Important Community update: The process for changing your account details was updated on June 25th. Learn how this impacts your Community experience and the actions we suggest you take to secure your account here.

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Alteryx - Automate Generating of DOCX files with python

Emil_Kos
17 - Castor
17 - Castor

Hi Everyone, 

Is it possible to use the data from an Alteryx Table Tool and include that information in a DOCX template? I am referring to the use case where Python would also be used.

 

I think that such a solution might not work, and someone would need to recreate the entire DOCX template in Python. Am I correct?

4 REPLIES 4
apathetichell
18 - Pollux

I'm going with a "this is totally possible" answer. Here are my caveats:

 

I've never done this. I use Word incredibly sparingly. I use Python only when I have to. You may need to create a temporary html file via render - and use that as your input vs the html report snippet produced via table tool.  (you might be able to use table tool as well - but you'd probably have to use plotly or something to format/correct it).

 

Can you do it off of the full html produced by Render?

nickmartella
7 - Meteor

I haven't used the table tool and im not sure if this would help in your case but it might. I have used the python tool to read data from incoming alteryx stream and used the shutil package to create copies of a template file, naming the files based on the values in the alteryx data and adding it to a combination to check and avoid making duplicate files. We send out automated emails every week to vendors using a specific template and this creates each file for us to send weekly. 

 

Once the template file copies are made, you then hard key the full path with formula tool in the data based on the condition you named that file in the python code(our case was the unique vendor name) and then configure an output tool to use that field and output the data on the specific copy. 

Emil_Kos
17 - Castor
17 - Castor

Thank you both for the answers. I will explore the topic a bit more.

 

I wonder, @nickmartella, if you could present to me the solution you have developed? I am curious about what is possible in this area when we combine Python and Alteryx.

nickmartella
7 - Meteor

 

 

from ayx import Alteryx
import shutil
import os

df = Alteryx.read("#1")

template_file = r"point to your docx template"
output_directory = r"folder location for files"

seen_vendors = set()  # set to keep record of vendor names

for i, row in df.iterrows():
    vendor_name = row["PO_VENDOR_NAME"] # reads this column in your alteryx data to create files

    if vendor_name not in seen_vendors:  # Check for duplicates
        seen_vendors.add(vendor_name)  # Add vendor name to the set if unique

        new_file_name = f" (DESIRED FILE NAME).docx"
        new_file_path = os.path.join(output_directory, new_file_name)
        shutil.copy(template_file, new_file_path)

 

 

You can use this code in a python tool to create your files, then do your data work with the reporting tools. I would add a block until done, with this code as the first output and then the rest of the workflow on the 2nd output. Also in your new file name you can add the name of what makes it unique. Ours was vendor name, so it was 'file name - {vendor_name}.docx' 

 

the best part about this method is you only have to make the template file once, then then you can do all the extra stuff in the workflow on your 2nd output from the block until done. 

Labels