在Python中导出PDF文件,你可以使用 `reportlab` 库。以下是使用 `reportlab` 库导出PDF的基本步骤和示例代码:
1. 安装 `reportlab` 库:
```bash
pip install reportlab
2. 编写Python代码生成PDF文件:
```python
from reportlab.pdfgen import canvas
def create_pdf(filename):
pdf_file = filename
c = canvas.Canvas(pdf_file)
在PDF上写入文字
c.drawString(100, 750, "Hello, World!")
保存PDF
c.save()
print(f"PDF文件 {pdf_file} 已生成!")
调用函数生成PDF
create_pdf("hello_world.pdf")
以上代码会在当前目录下生成一个名为 `hello_world.pdf` 的PDF文件,其中包含文本 "Hello, World!"。
如果你需要更复杂的PDF文档,比如包含图像、表格等,`reportlab` 库也提供了相应的功能。