【问题标题】:Saving Pandas DataFrame into PDF File format without pdfkit将 Pandas DataFrame 保存为没有 pdfkit 的 PDF 文件格式
【发布时间】:2019-01-29 03:16:02
【问题描述】:

我想将 pandas 数据框保存为 pdf 格式。

import pdfkit as pdf    
config = pdf.configuration(wkhtmltopdf="C:\Program Files\wkhtmltopdin\wkhtmltopdf.exe")
    pdf.from_url('http://google.com', 'out.pdf',configuration=config)
--> not working somehow even though I downloaded wkhtmltopdin on several different locations 

from weasyprint import HTML
HTML(string=pd.read_csv('cor.csv').to_html()).write_pdf("report.pdf")

dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2
--> not working : Tried several times to solve this isseue, but cannot download library

我在stackoverflow和其他网站上尝试了5个以上的包和方法,但都无法解决。

还有更多我可以尝试的软件包吗?这让我得了癌症

提前致谢。

【问题讨论】:

    标签: python pandas dataframe pdf


    【解决方案1】:

    一种选择是从以下开始:

    df.to_html()
    

    然后使用QT将HTML转成PDF如下:

    from PyQt4.QtGui import QTextDocument, QPrinter, QApplication
    
    import sys
    app = QApplication(sys.argv)
    
    doc = QTextDocument()
    location = "c://apython//Jim//html//notes.html"
    html = open(location).read()
    doc.setHtml(html)
    
    printer = QPrinter()
    printer.setOutputFileName("foo.pdf")
    printer.setOutputFormat(QPrinter.PdfFormat)
    printer.setPageSize(QPrinter.A4)
    printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter)
    
    doc.print_(printer)
    print("done!")
    

    我从html to pdf获得了第二段代码,并在Mac OSX上进行了测试,结果是肯定的。

    【讨论】:

      【解决方案2】:

      您是否考虑过绘制一个 Matplotlib 表格,然后导出表格图?

      import matplotlib.backends.backend_pdf
      import matplotlib.pyplot as plt
      import pandas as pd
      
      d = {'x{}'.format(i): range(30) for i in range(10)}
      
      table = pd.DataFrame(d)
      
      fig = plt.figure()
      
      ax=fig.add_subplot(111)
      
      cell_text = []
      for row in range(len(table)):
          cell_text.append(table.iloc[row])
      
      ax.table(cellText=cell_text, colLabels=table.columns, loc='center')
      ax.axis('off')
      
      pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
      pdf.savefig(fig)
      pdf.close()
      
      

      我发现这很简单,高度可定制且独立于操作系统(据我所知)。我能够在客户端的服务器上实现这一点,而无需下载任何额外的包。

      【讨论】:

        猜你喜欢
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-07
        • 2011-07-06
        • 2016-11-26
        • 1970-01-01
        • 2016-11-14
        相关资源
        最近更新 更多