Saving Pandas DataFrame into PDF File format without pdfkit

后端 未结 2 1943
渐次进展
渐次进展 2021-01-06 08:16

I want to save a pandas dataframe into pdf format.

import pdfkit as pdf    
config = pdf.configuration(wkhtmltopdf=\"C:\\Program Files\\wkhtmltopdin\\wkhtmlt         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 08:39

    One option is to start with:

    df.to_html()
    

    and then use QT to convert the HTML to PDF as follows:

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

    I obtained the second bit of code from html to pdf, and tested on Mac OSX with positive results.

提交回复
热议问题