use*_*167 16
第一个绘图表,matplotlib然后生成 pdf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))
#/sf/ask/2249617751/
fig, ax =plt.subplots(figsize=(12,4))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')
#/sf/ask/282953471/
pp = PdfPages("foo.pdf")
pp.savefig(fig, bbox_inches='tight')
pp.close()
Run Code Online (Sandbox Code Playgroud)
参考:
小智 12
这是我如何使用 sqlite3、pandas 和pdfkit从 sqlite 数据库中执行此操作
import pandas as pd
import pdfkit as pdf
import sqlite3
con=sqlite3.connect("baza.db")
df=pd.read_sql_query("select * from dobit", con)
df.to_html('/home/linux/izvestaj.html')
nazivFajla='/home/linux/pdfPrintOut.pdf'
pdf.from_file('/home/linux/izvestaj.html', nazivFajla)
Run Code Online (Sandbox Code Playgroud)
小智 8
参考这两个我发现有用的例子:
简单的 CSS 代码保存在与 ipynb 相同的文件夹中:
/* includes alternating gray and white with on-hover color */
.mystyle {
font-size: 11pt;
font-family: Arial;
border-collapse: collapse;
border: 1px solid silver;
}
.mystyle td, th {
padding: 5px;
}
.mystyle tr:nth-child(even) {
background: #E0E0E0;
}
.mystyle tr:hover {
background: silver;
cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇代码:
pdf_filepath = os.path.join(folder,file_pdf)
demo_df = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))
table=demo_df.to_html(classes='mystyle')
html_string = f'''
<html>
<head><title>HTML Pandas Dataframe with CSS</title></head>
<link rel="stylesheet" type="text/css" href="df_style.css"/>
<body>
{table}
</body>
</html>
'''
HTML(string=html_string).write_pdf(pdf_filepath, stylesheets=["df_style.css"])
Run Code Online (Sandbox Code Playgroud)
一种方法是使用降价。您可以使用df.to_html(). 这会将数据框转换为 html 表。从那里您可以将生成的 html 放入降价文件 (.md)(请参阅http://daringfireball.net/projects/markdown/basics)。从那里,有一些实用程序可以将 Markdown 转换为 pdf ( https://www.npmjs.com/package/markdown-pdf )。
这种方法的一种多合一工具是使用 Atom 文本编辑器 ( https://atom.io/ )。在那里您可以使用扩展名,搜索“markdown to pdf”,这将为您进行转换。
注意:to_html()最近使用时,由于某种原因,我不得不删除额外的 '\n' 字符。我选择使用Atom -> Find -> '\n' -> Replace "".
总的来说,这应该可以解决问题!
这是一个带有中间 pdf 文件的解决方案。
该表用一些最小的 css 打印得很漂亮。
pdf 转换是通过 weeasyprint 完成的。你需要pip install weasyprint。
# Create a pandas dataframe with demo data:
import pandas as pd
demodata_csv = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
df = pd.read_csv(demodata_csv)
# Pretty print the dataframe as an html table to a file
intermediate_html = '/tmp/intermediate.html'
to_html_pretty(df,intermediate_html,'Iris Data')
# if you do not want pretty printing, just use pandas:
# df.to_html(intermediate_html)
# Convert the html file to a pdf file using weasyprint
import weasyprint
out_pdf= '/tmp/demo.pdf'
weasyprint.HTML(intermediate_html).write_pdf(out_pdf)
# This is the table pretty printer used above:
def to_html_pretty(df, filename='/tmp/out.html', title=''):
'''
Write an entire dataframe to an HTML file
with nice formatting.
Thanks to @stackoverflowuser2010 for the
pretty printer see /sf/answers/3340633131/
'''
ht = ''
if title != '':
ht += '<h2> %s </h2>\n' % title
ht += df.to_html(classes='wide', escape=False)
with open(filename, 'w') as f:
f.write(HTML_TEMPLATE1 + ht + HTML_TEMPLATE2)
HTML_TEMPLATE1 = '''
<html>
<head>
<style>
h2 {
text-align: center;
font-family: Helvetica, Arial, sans-serif;
}
table {
margin-left: auto;
margin-right: auto;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: center;
font-family: Helvetica, Arial, sans-serif;
font-size: 90%;
}
table tbody tr:hover {
background-color: #dddddd;
}
.wide {
width: 90%;
}
</style>
</head>
<body>
'''
HTML_TEMPLATE2 = '''
</body>
</html>
'''
Run Code Online (Sandbox Code Playgroud)
感谢@stackoverflowuser2010提供了漂亮的打印机,请参阅stackoverflowuser2010的答案/sf/answers/3340633131/
我没有使用 pdfkit,因为我在无头机器上遇到了一些问题。但 weeasyprint 很棒。
| 归档时间: |
|
| 查看次数: |
25038 次 |
| 最近记录: |