我是python和reportlab的新手,但正在尝试生成一个PDF文件,并在其中写入我的主机名。
这是我的代码和标题。如何打印我的主机名并使用它生成PDF?
#!/usr/bin/python
from reportlab.pdfgen import canvas
def hello():
c = canvas.Canvas("helloworld.pdf")
c.drawString(250,800,'Hello world')
c.showPage()
c.save()
hello()发布于 2015-01-29 07:45:51
既然你没有提到你的平台(win,macos,linux),那么我将给你一个在python中找到你的主机名的通用方法。
要获取主机名,可以使用套接字库和其中的gethostname函数,因此最终的函数将如下所示:
from reportlab.pdfgen import canvas
from socket import gethostname
def hello():
c = canvas.Canvas("hostname.pdf")
c.drawString(250,800,gethostname())
c.save()
hello()https://stackoverflow.com/questions/28197240
复制相似问题