Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Qt/pyQt/PySide-创建用于打印的文档模板_Python_C++_Qt_Pyqt_Pyside - Fatal编程技术网

Python Qt/pyQt/PySide-创建用于打印的文档模板

Python Qt/pyQt/PySide-创建用于打印的文档模板,python,c++,qt,pyqt,pyside,Python,C++,Qt,Pyqt,Pyside,你好 我是新来的,这是我的第一个小项目,所以要理解。 Im需要为一个文档创建一个布局,该文档将用作模板,最后将填充从数据库收集的数据并打印出来。尚未创建数据库 所以,现在,我成立了当地的VAR来解决这个问题。我想要每一个 将数据块放在非常特定的位置。我检查了QTextDocument文档,它看起来像 这是一个很好的工作工具,但我不知道如何为它创建布局。非常感谢任何指针。 编辑:网络上关于这个主题的内容不多。虽然我能找到 这几乎就是我要找的。然而,我希望将许多变量放入html中,因此“.forma


你好
我是新来的,这是我的第一个小项目,所以要理解。 Im需要为一个文档创建一个布局,该文档将用作模板,最后将填充从数据库收集的数据并打印出来。尚未创建数据库 所以,现在,我成立了当地的VAR来解决这个问题。我想要每一个 将数据块放在非常特定的位置。我检查了QTextDocument文档,它看起来像 这是一个很好的工作工具,但我不知道如何为它创建布局。
非常感谢任何指针。

编辑:网络上关于这个主题的内容不多。虽然我能找到


这几乎就是我要找的。然而,我希望将许多变量放入html中,因此“.format”选项似乎没有什么压倒性。有人知道更好的解决方案吗?

Qt中有许多支持HTML的小部件。QTextDocument应该可以工作,QLabel也可以。 在我的项目中,我使用了QtWebKit.QWebView,因为我需要表格,而我认为其他小部件没有这样做(但我不确定,如果需要,请查看文档)

在我看来,如果您使用命名参数,“.format”并不是压倒性的,这对于我来说已经足够好了。为参数值使用字典也可能有帮助

示例代码:

from PyQt4 import QtGui
from PyQt4 import QtWebKit

class DisplayHTML(QtWebKit.QWebView):

    def __init__(self, html, parent=None):
        super().__init__(parent)
        self.setHtml(html)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    html_template = "<html><p>Hello, {first}, how {second} you {third}?</p></html>"
    values_dict = {
        'first': 'Joe',
        'second': 'are',
        'third': 'today',
    }
    html_ready_to_render = html_template.format(**values_dict)
    window = DisplayHTML(html_ready_to_render)
    window.show()
    sys.exit(app.exec_())
从PyQt4导入QtGui
从PyQt4导入QtWebKit
类DisplayHTML(QtWebKit.QWebView):
def uuu init uuuu(self,html,parent=None):
super()。\uuuu init\uuuu(父级)
self.setHtml(html)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
导入系统
app=QtGui.QApplication(sys.argv)
html_template=“你好,{first},你好{second}你{third}吗?

” 值\u dict={ “第一个”:“乔”, '第二个':'是', "三":"今天",, } html\u ready\u to\u render=html\u template.format(**values\u dict) window=DisplayHTML(html已准备好呈现) window.show() sys.exit(app.exec_())
编辑:你不需要创建一个新的类来完成这么简单的事情,只要使用一个QWebView()并调用setHtml()

从PyQt4导入QtGui
从PyQt4导入QtWebKit
类DisplayHTML(QtWebKit.QWebView):
def uuu init uuuu(self,html,parent=None):
super()。\uuuu init\uuuu(父级)
self.setHtml(html)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
导入系统
app=QtGui.QApplication(sys.argv)
html_template=“你好,{first},你好{second}你{third}吗?

” 值\u dict={ “第一个”:“乔”, '第二个':'是', "三":"今天",, } html\u ready\u to\u render=html\u template.format(**values\u dict) window=DisplayHTML(html已准备好呈现) window.show() sys.exit(app.exec_())
这非常有用。老实说,我不知道我可以使用命名参数和dict选项,太棒了。只有一个问题tho,参数序列必须保留吗?或者我可以先用“第三个”“第二个”得到相同的输出?哦,非常感谢!不客气。顺序在关键字参数(即myfunc(first='Joe',second='something')中不重要;在位置参数(即myfunc('Joe','something')中重要。这是python语言的功能,不是特定于格式函数的功能,您可以选择使用关键字或位置参数。使用myfunc(*some dict)将一些\u dict解压到函数的关键字参数中。参数解压也是一种语言功能,使用*my\u list解压列表或元组或类似的iterable,使用**my\u dict解压像dict这样的哈希表类型。您应该添加一些说明
from PyQt4 import QtGui
from PyQt4 import QtWebKit

class DisplayHTML(QtWebKit.QWebView):

    def __init__(self, html, parent=None):
        super().__init__(parent)
        self.setHtml(html)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    html_template = "<html><p>Hello, {first}, how {second} you {third}?</p></html>"
    values_dict = {
        'first': 'Joe',
        'second': 'are',
        'third': 'today',
    }
    html_ready_to_render = html_template.format(**values_dict)
    window = DisplayHTML(html_ready_to_render)
    window.show()
    sys.exit(app.exec_())