首页 > 解决方案 > 在转换第三列时将 excel 转换为 pdf 问题

问题描述

我想将我的 excel 文件转换为 pdf,但在转换第三列第三列时出现问题,请帮我解决这个问题

预期结果

索引列名姓氏性别

1个

2 dd gg 米

实际的

索引名字姓氏

1 性别

2```dd gg

将熊猫导入为 pd

从 fpdf 导入 FPDF

df_2 = pd.read_excel('exceltopdf.xlsx')

pdf = FPDF()

pdf.add_page()

pdf.set_xy(0, 0)

pdf.set_font('arial', 'B', 14)

pdf.cell(60)

pdf.cell(70, 10, '从 python 编写 PDF', 0, 4, 'C')

pdf.cell(-40)

pdf.cell(50, 10, '索引列', 1, 0, 'C')

pdf.cell(40, 10, '名字', 1, 0, 'C')

pdf.cell(40, 10, '姓氏', 1, 2, 'C')

pdf.cell(40, 10, '性别', 1, 3, 'C')

pdf.cell(-90)

pdf.set_font('arial', '', 12)

对于范围内的我(0,len(df_2)-1):

col_ind = str(i)

col_a = str(df_2['First Name'].ix[i])

col_b = str(df_2['Last Name'].ix[i])

col_c = str(df_2['Gender'].ix[i])

pdf.cell(50, 10, '%s' % (col_ind), 1, 0, 'C')

pdf.cell(40, 10, '%s' % (col_a), 0, 0, 'C')

pdf.cell(40, 10, '%s' % (col_b), 0, 2, 'C')

pdf.cell(40, 10, '%s' % (col_c), 0, 4, 'C')

pdf.cell(-90)
pdf.output('test.pdf', 'F')

标签: pythonpdf-generation

解决方案


推荐阅读