我一直在尝试创建将我计算机上的 .db 文件转换为 .pdf 文件的代码。我制作的代码有效,但 pdf 文件的格式完全关闭。 pdf 的左侧看起来很正常,但它跑到了右侧,您甚至看不到它说的是什么。这是我当前的代码:
import sqlite3
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
conn = sqlite3.connect(r'C:\Users\ethan\Cookies\Desktop\MattressFirm\BedTimes\bedtimes.db')
cur = conn.cursor()
cur.execute('SELECT * FROM articles')
rows = cur.fetchall()
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
page_width, page_height = letter
left_margin = 25
right_margin = 25
top_margin = 25
bottom_margin = 25
column_width = (page_width - left_margin - right_margin) / len(rows[0])
pdf_file = canvas.Canvas('output.pdf', pagesize=letter)
y = page_height - top_margin
for row in rows:
x = left_margin + ((page_width - left_margin - right_margin) - (column_width * len(row))) / 2
for value in row:
pdf_file.setFont('Arial', 10)
pdf_file.drawString(x, y, str(value))
x += column_width
y -= 15
if y < bottom_margin:
pdf_file.showPage()
y = page_height - top_margin
pdf_file.save()
conn.close()
有人知道如何修复格式吗?
编辑:我认为提及我正在测试的特定 .db 文件只有 1 列也会有所帮助。