banner



How Can I Add A Template To Rfi In Plangrid?

Generating Custom Word Documents From Templates Using Python

A few weeks agone, I started an heady yet challenging project. The idea was to generate notification email attachments (word document or PDF) containing responses from a grade owner'south respondents using a custom word document template provided past the form possessor.

Now that I am done with information technology, I would like to share a guide to implementing a feature similar this and how to avert some pitfalls I discovered in the process. At the end of this article, you will be able to generate your own custom Discussion documents from templates, and even add new stuff to existing documents.

Why would I want to Generate Custom Word Documents using Python?

I know, I know. Why become through the trouble of writing code to generate custom word documents when yous can just create the document yourself?

Imagine if yous have to type out transcripts for hundreds or thousands of students. What if you accept to write generic reports based on feedback from parents or your customers, or add your company's watermark to each page of a document (or hundreds of documents)? Non looking so like shooting fish in a barrel anymore, is it?

Having an application that generates these documents using templates would make things a lot easier and quicker for y'all. It'south likewise a fun project to embark on if you are grooming to be a software developer.

What do I Need to Go Started?

You would need:

  1. Basic knowledge of Python. I used Python 2.vii in this commodity.
  2. Python installed on your machine. If you oasis't installed Python before, read this for instructions.
  3. Python web application. If you don't have one, you could use a framework (Flask or Django) to create ane. I used the Flask framework considering information technology'due south a lightweight framework and we don't need much. Yous can learn how to create a uncomplicated Flask awarding here.
  4. A Package manager for Python. I would recommend using pip. You lot tin find instructions on how to install pip here.

Do I demand whatever External Libraries?

Yes, yous need two:

  1. python-docx. This is a very powerful library used for creating give-and-take documents with basically all the elements you demand — images, header, footer, page breaks, etc. While this library is great for creating docx files, it's not very good at modifying them. We need this library in society to set the dimensions of whatsoever image nosotros might add to the document, every bit you will see after on.
  2. python-docx-template. This library is designed to generate documents using word documents every bit jinja templates. It makes slotting in custom values into a give-and-take document a lot easier. You tin notice more information in this article on library documentation here.

In social club to install both dependencies, apply the command prompt to navigate to the directory where y'all want to relieve your lawmaking and run the post-obit commands:

          pip install docx          pip install docxtpl        

Creating a Word Document Template

Permit's say we want to design an invoice generation organisation. Nosotros would need a Give-and-take Document as a template. I created a sample template. Here's what it looks like:

For the purpose of simplicity, I'll be using the following tags (there are more than tags in the python-docx-template library documentation article):

It is important to note that y'all should not put the same tag twice in a run, paragraph, table column or row. The library documentation explains better.

Generating Document From Word Template

Preparing Template Context

The context contains the information nosotros would like to add to our document. It is a lexicon with key-value pairs, where the central is the proper noun of the variable we included in our template and the value is what we would like to substitute the variable with. Nosotros are calculation single variables, an array of elements for the for-loop and an inline epitome.

Unmarried Variable

We would need the following variables for our invoice template:

Our context should look similar this:

For Loops

The context for adding data to the Word document using for loops takes the form of an array of elements. For example, in our invoice, nosotros would like to add together all the items purchased to the table. We already have a for loop in the template that looks similar this:

The variable row_contents will hold the assortment of elements nosotros need to add to the table equally rows.

If our customer bought:

  1. xxx dozens of eggs at $5 for each dozen. The total corporeality is $150.
  2. 10 bags of all-purpose flour at $15 for each bag. The total is $150.
  3. 5 bags of granulated sugar at $7 each. The full amount is $35.

Our final context with all the required variables (except signature — we'll get to that later) should wait like this:

Signature

You could create a split role that adds the signature to the context yous already accept. It could expect like this:

Please notation that the signature argument could be either a file path or a string buffer that holds the signature image.

Bringing It Together — Generating the Word Document

The lawmaking for generating the word document tin be institute in this github repo.

Hither's how the document should look similar when downloaded:

Tips

  1. You tin can open your Word document template using a file path, as shown in this article, or a cord buffer containing the word certificate (in case you programme to get your Word template from a database). The latter eliminates the need for the template to be in the same directory as the Python script.
  2. In order to add an inline images to your Word document, ensure that your images are in bmp, gif, jpeg, tiff or png file formats. The library just supports these file formats for images.

You can set the size of your inline paradigm using Cm, Inches, Mm, Emu, Pts, and Twips. Only import them from the docx.shared module like this:

Wrapping Upwards

This projection is a dandy way to learn and do coding using Python, components of Give-and-take documents and file processing. It likewise helps automate simple tasks, saving time and energy.

If you desire to learn how to accept a spider web folio offline using service workers, this article on the Formplus blog is a great guide. Take a wait and allow me know how it goes.

Source: https://blog.formpl.us/how-to-generate-word-documents-from-templates-using-python-cb039ea2c890

Posted by: kaufmananempon.blogspot.com

0 Response to "How Can I Add A Template To Rfi In Plangrid?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel