python - wxPython 打印带格式的字符串

标签 python printing tkinter wxpython

所以我有下面的类,它打印您用它调用的文本和标题。我还提供了用于调用打印函数的代码。我有一个字符串“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))

预期打印:

+-------------------+---------------------------------+------+-----------------+-----------+
|      Domain:      |           Mail Server:          | TLS: | # of Employees: | Verified: |
+-------------------+---------------------------------+------+-----------------+-----------+
| bankofamerica.com |    ltwemail.bankofamerica.com   |  Y   |      239000     |     Y     |
|                   |    rdnemail.bankofamerica.com   |  Y   |                 |     Y     |
|                   |    kcmemail.bankofamerica.com   |  Y   |                 |     Y     |
|                   |    rchemail.bankofamerica.com   |  Y   |                 |     Y     |
|   citigroup.com   |        mx-b.mail.citi.com       |  Y   |      248000     |     N     |
|                   |        mx-a.mail.citi.com       |  Y   |                 |     N     |
|   bnymellon.com   |  cluster9bny.us.messagelabs.com |  ?   |      51400      |     N     |
|                   | cluster9bnya.us.messagelabs.com |  Y   |                 |     N     |
|     usbank.com    |         mail1.usbank.com        |  Y   |      65565      |     Y     |
|                   |         mail2.usbank.com        |  Y   |                 |     Y     |
|                   |         mail3.usbank.com        |  Y   |                 |     Y     |
|                   |         mail4.usbank.com        |  Y   |                 |     Y     |
|    us.hsbc.com    |       vhiron1.us.hsbc.com       |  Y   |      255200     |     Y     |
|                   |       vhiron2.us.hsbc.com       |  Y   |                 |     Y     |
|                   |       njiron1.us.hsbc.com       |  Y   |                 |     Y     |
|                   |       njiron2.us.hsbc.com       |  Y   |                 |     Y     |
|                   |       nyiron1.us.hsbc.com       |  Y   |                 |     Y     |
|                   |       nyiron2.us.hsbc.com       |  Y   |                 |     Y     |
|      pnc.com      |   cluster5a.us.messagelabs.com  |  Y   |      49921      |     N     |
|                   |   cluster5.us.messagelabs.com   |  ?   |                 |     N     |
|     tdbank.com    |   cluster5.us.messagelabs.com   |  ?   |        0        |     N     |
|                   |   cluster5a.us.messagelabs.com  |  Y   |                 |     N     |
+-------------------+---------------------------------+------+-----------------+-----------+

实际打印:

与预期相同,但删除了空格,使其很难阅读。

函数调用:

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

最佳答案

对于其他陷入困境的人来说,这是我用来生成包含所有行和列的漂亮表格的修改后的类函数。

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>"

关于python - wxPython 打印带格式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24148387/

相关文章:

python - 评级标准化

python - 如何在Python中读取多行输入?

mysql - 在mysql语句中回显?

python - 显示存储在函数中的图像

python - 在文本小部件中标记 - tkinter

python - 在Python中模拟SHL和SHR ASM指令

python - Beautiful Soup - 打印容器文本而不打印子元素的文本

python - 将自定义按钮添加到 NavigationToolbarTkAgg

python - tensorflow 偏差和权重变量

c - 在 C 中打印原始文本时如何捕获错误