pdfpcell输出换行
时间: 2024-01-07 17:05:26 浏览: 103
itext生成pdf解决中英文换行问题
在使用 `pdfpcell` 输出时,可以使用 `\n` 或 `\r\n` 来表示换行。例如:
```
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.enums import TA_CENTER
styles = getSampleStyleSheet()
data = [['Name', 'Age', 'Gender'],
['Tom', '20', 'Male'],
['Jerry', '19', 'Female\n'],
['Mary', '22', 'Female']]
table = Table(data)
table.setStyle(TableStyle([('BACKGROUND', (0,0), (-1,0), colors.gray),
('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke),
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
('FONTSIZE', (0,0), (-1,0), 14),
('BOTTOMPADDING', (0,0), (-1,0), 12),
('BACKGROUND', (0,1), (-1,-1), colors.beige),
('TEXTCOLOR', (0,1), (-1,-1), colors.black),
('FONTNAME', (0,1), (-1,-1), 'Helvetica'),
('FONTSIZE', (0,1), (-1,-1), 12),
('BOTTOMPADDING', (0,1), (-1,-1), 10),
('BACKGROUND', (0,-1), (-1,-1), colors.gray),
('TEXTCOLOR', (0,-1), (-1,-1), colors.whitesmoke),
('FONTNAME', (0,-1), (-1,-1), 'Helvetica-Bold'),
('FONTSIZE', (0,-1), (-1,-1), 14),
('TOPPADDING', (0,-1), (-1,-1), 12)]))
pdf_file = 'table.pdf'
pdf = SimpleDocTemplate(pdf_file, pagesize=landscape(A4))
pdf.build([table])
```
在上面的例子中,第三行数据的 Gender 列使用了 `\n` 换行,输出时会自动换行。
阅读全文