wxPython打印带有格式的字符串

时间:2014-06-10 18:30:46

标签: python printing tkinter wxpython

所以我有下面的类打印你用它调用的文本和标题。我还提供了用于调用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))

预期打印:

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

也许试试

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

用html空格字符替换你的空格......但它仍然看起来不正确,因为它不是等宽字体...这很难自动化...你真的想把它放在一个表结构......但这需要一些工作

你可能想在你的html转换中投入更多的时间......或许类似的事情(根据你所展示的内容做出假设

def GetHtmlText(self,text):
    "Simple conversion of text.  Use a more powerful version"
    text_lines = text.splitlines()
    html_text = "<table>"
    html_text += "<tr><th> + "</th><th>".join(text_lines[0].split(":")) + "</th></tr>
    for line in text_lines[1:]:
       html_text += "<tr><td>"+"</td><td>".join(line.split()) +"</td></tr>
    return html_text + "</table>"
相关问题