Is it possible to have python output whole documents with specific formats using a question-answer prompt?

Viewed 41

Is it possible for python to output whole documents with a specific format? And could I possibly have it do this by means of a question/answer prompt? Here's an example of what I would like it to do.

Let's say I wanted to create a document on storm analysis. I'd like to start a "script" where Python would ask me some questions such as:

  • What is the storm name? (Enter storm name, hit enter)
  • What is the current approximate location? (Enter storm location, hit enter)
  • What is the current pressure? (Enter pressure value, hit enter)
  • What is the wind direction summary? (Enter summary, hit enter)
  • What images do you have for wind? (Enter png file, or something similar, hit enter)

So I would like it to do "ask" me all these questions for my complete document, then output it in a format that I want in say a pdf or word document. Here's an example of an older format (I may not explicitly want this, but this is just a generic example I'd hope to be able to program into Python): enter image description here

Is this type of thing possible for python? Or is this better left to another program?

1 Answers

One very common way of doing this is by using something like flask/jinja to create an html doc. From there you can convert the page to a pdf/image/whatever using other tools.

With a project structure like

-app.py
-templates
    -template.html

You could write the following to generate a report:

app.py:

from flask import Flask, render_template

app = Flask(__name__)

if __name__ == "__main__":
    user_input = input("Type a message")
    with app.app_context():
        with open("example.html", "w") as f:
            f.write(render_template("template.html", message=user_input))

template.html:

<html>
    <head>
        <title>Jinja Test</title>
    </head>
    <body>
        <span>{{ message }}</span>
    </body>
</html>

When you type in a message, say "hi" for example, an html page in example.html will thus be generated that looks like this:

<html>
    <head>
        <title>Jinja Test</title>
    </head>
    <body>
        <span>hi</span>
    </body>
</html>

You could thus use this to customize whatever report you want using a combination of html/css and inputs.

Related