我有一个数据框表。我想将其保存在 PDF 页面中。该表的列名很长,当我打印它时,表超出了边距并且数据丢失。此外,我看不到此处打印的行索引:
我的数据:
prdf=
group FF_ThisWeek FF_ThreeMonths ifrac_ThisWeek ifrac_ThreeMonths vfrac_ThisWeek vfrac_ThreeMonths
6.A-3.2.10 0.75 0.76 0.90 0.90 0.84 0.84
6.A-3.2.11 0.71 0.70 0.85 0.83 0.83 0.85
6.A-3.2.12 0.77 0.76 0.92 0.91 0.84 0.84
6.A-3.2.2 0.75 0.76 0.93 0.92 0.81 0.83
6.A-3.2.4 0.74 0.75 0.91 0.91 0.81 0.83
6.A-3.2.5 0.75 0.76 0.90 0.90 0.84 0.85
6.A-3.2.6 0.75 0.76 0.92 0.91 0.82 0.83
6.A-3.2.8 0.76 0.75 0.92 0.90 0.82 0.83
# code
from fpdf import FPDF
pdf=FPDF()
pdf.add_page()
pdf.set_font('arial', 'B', 11)
### Where to start on the page, x-position
pdf.cell(60)
pdf.cell(75, 10,"Table: FF, Vmpp/Voc,Impp/Ioc in this week and last three months", 0, 2, 'C')
pdf.cell(90, 5, '', 0, 2, 'C')
pdf.cell(-55)
columnNameList = list(prdf.columns)
for header in columnNameList[:-1]:
pdf.cell(35,10, header, 1, 0, 'L')
pdf.cell(35, 10, columnNameList[-1], 1, 2, 'C')
pdf.cell(-140)
pdf.set_font('arial', '', 11)
for row in range(0, len(prdf)):
for col_num, col_name in enumerate(columnNameList):
# print(df['%s' % (col_name)].iloc[row])
if col_num != len(columnNameList) - 1:
pdf.cell(35, 10, str(prdf['%s' % (col_name)].iloc[row]), 1, 0, 'C')
else:
pdf.cell(35, 10, str(prdf['%s' % (col_name)].iloc[row]), 1, 2, 'C')
pdf.cell(-140)
pdf.cell(35, 10, "", 0, 2)
pdf.cell(20)
# pdf.image('iris_grouped_df.png', x = None, y = None, w = 0, h = 0, type = '', link = '')
pdf.output(pdf_name2, 'F')
目前的输出: