wxPython打印带格式的字符串

2024-04-26 01:31:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有了下面的类,它打印你用它调用的文本和标题。我还提供了我用来调用Print函数的代码。我有一个字符串“outputstring”,其中包含我要打印的文本。我的预期产出低于实际产出。它似乎在删除一些需要适当易读性的空格。如何在保留空格的同时打印?在

班级:

#Printer Class
class Printer(HtmlEasyPrinting):
    def __init__(self):
        HtmlEasyPrinting.__init__(self)

    def GetHtmlText(self,text):
        "Simple conversion of text.  Use a more powerful version"
        html_text = text.replace('\n\n','<P>')
        html_text = text.replace('\n', '<BR>')
        return html_text

    def Print(self, text, doc_name):
        self.SetHeader(doc_name)
        self.PrintText(self.GetHtmlText(text),doc_name)

    def PreviewText(self, text, doc_name):
        self.SetHeader(doc_name)
        HtmlEasyPrinting.PreviewText(self, self.GetHtmlText(text))

预期打印:

^{pr2}$

实际打印:

和预期的一样,但是空格被去掉了,很难阅读。在

函数调用:

def printFile():
    outputstring = txt_tableout.get(1.0, 'end')
    print(outputstring)
    app = wx.PySimpleApp()
    p = Printer()
    p.Print(outputstring, "Data Results")

Tags: textname文本selfdocinitdefhtml
2条回答

也许可以试试

`html_text = text.replace(' ','&nbsp;').replace('\n','<br/>')`

这将用html空格字符替换空格。。。但它看起来还是不对,因为它不是等宽字体。。。这很难自动化。。。你真的想把它放到一个表结构中。。。但这需要一些工作

你可能想在你的html转换上多花点时间。。。或许是这样的(根据你的表现做出假设)

^{pr2}$

对于其他正在挣扎的人,这是我用来生成一个包含所有行和列的漂亮表的修改后的类函数。在

def GetHtmlText(self,text):
    html_text = '<h3>Data Results:</h3><p><table border="2">'
    html_text += "<tr><td>Domain:</td><td>Mail Server:</td><td>TLS:</td><td># of Employees:</td><td>Verified</td></tr>"
    for row in root.ptglobal.to_csv():
        html_text += "<tr>"
        for x in range(len(row)):
            html_text += "<td>"+str(row[x])+"</td>"
        html_text += "</tr>"
    return html_text + "</table></p>"

相关问题 更多 >