是否可以使用问题提示提示Python以特定格式输出整个文档?

发布于 2025-02-01 04:00:45 字数 594 浏览 5 评论 0原文

Python是否有可能以特定格式输出整个文档?我是否可以通过问候/答案提示来完成此操作?这是我想要做的示例。

假设我想创建有关风暴分析的文档。我想启动一个“脚本”,python会问我一些问题,例如:

  • 风暴名称是什么? (输入风暴名称,命中输入)
  • 当前大约位置是什么? (输入风暴位置,命中输入)
  • 当前压力是多少? (输入压力值,点击输入)
  • 什么是风向摘要? (输入摘要,命中输入)
  • 您有哪些风图? (输入PNG文件或类似的内容,请输入),

因此我希望它为完整的文档“向我询问”所有这些问题,然后以我想要的PDF或Word文档的格式输出。以下是较旧格式的示例(我可能不明确想要这个,但这只是一个通用的示例,我希望能够编程为Python):

Python可能会有这种类型的东西吗?还是最好留给另一个程序?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂人 2025-02-08 04:00:45

这样做的一种非常普遍的方法是使用诸如烧瓶/jinja之类的东西来创建HTML文档。从那里,您可以使用其他工具将页面转换为PDF/Image/任何内容。

使用类似的项目结构,

-app.py
-templates
    -template.html

您可以编写以下内容来生成报告:

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>

当您输入消息时,例如,将生成example.html中的html页面。这样:

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

因此,您可以使用它来自定义想要使用HTML/CSS和输入组合的任何报告。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文