python操作系统不是经验值

2024-05-15 18:04:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用wkhtmltopfd工具和python生成一个非常简单的pdf:

# html string (works fine in all browsers)
html = generate_html()
f = open("/tmp/sudoku.html", 'w')
f.write(html)
system('wkhtmltopdf /tmp/sudoku.html sudoku.pdf')
f.close()

生成的pdf是空白的,但是如果我直接从命令行调用wkhtmltopfd,它可以工作:

^{pr2}$

为什么? 谢谢。在


Tags: 工具instringpdfhtmlopenalltmp
1条回答
网友
1楼 · 发布于 2024-05-15 18:04:48

除非/tmp/sudoku.html已经包含内容,否则需要先关闭file对象,然后再调用wkhtmltopdf从中生成PDF。除非flush已写入文件的内容,否则在关闭该文件之前,/tmp/sudoku.html不会输出任何内容。尝试:

html = generate_html()
f = open("/tmp/sudoku.html", 'w+')
f.write(html)
f.close() # close the file first
system('wkhtmltopdf /tmp/sudoku.html sudoku.pdf')

相关问题 更多 >