我正在尝试在(横向布局)pdf 上用 python 打印包含许多列(至少 6 列)的表格并包装列。 在这个generate_pdf2中,我遇到了编译错误,我自己无法成功解决:( 感谢您的帮助!
def generate_pdf2(df_list, titles, calculated_prices):
# Create a landscape layout PDF
doc = SimpleDocTemplate("pricing_information.pdf", pagesize=landscape(A4))
# Initialize a list to hold the elements of the PDF
elements = []
# Define styles for the paragraphs
styles = getSampleStyleSheet()
normal_style = styles["Normal"]
# Add title and information depending on variables
for title, calculated_price in zip(titles, calculated_prices):
elements.append(Paragraph(title, normal_style))
elements.append(Paragraph(f"Calculated Price: {calculated_price}", normal_style))
elements.append(Paragraph("", normal_style)) # Add empty line for spacing
# Add tables with DataFrame titles and calculated prices
for df, title, calculated_price in zip(df_list, titles, calculated_prices):
# Convert DataFrame to a list of lists for the table
data = [list(df.columns)] + df.astype(str).values.tolist()
# Adjust column widths to fit content within the page
# col_widths = [len(str(df[col].max())) * 12 for col in df.columns]
# col_widths = [float(width) for width in col_widths] # Convert to float
col_widths = [(float(len(str(df[col].max()))) * 12.0) for col in df.columns]
# Create a Table object with wrapped text
table = Table(data, colWidths=col_widths)
# Apply styles to the table
table.setStyle(TableStyle([('BACKGROUND', (0, 0), (-1, 0), 'lightgrey'),
('TEXTCOLOR', (0, 0), (-1, 0), 'black'),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), 'white'),
('GRID', (0, 0), (-1, -1), 1, 'black')]))
# Wrap text in cells
width, height = A4
for i in range(len(data)):
for j in range(len(data[i])):
table._argW[i][j] = col_widths[j] # Set cell widths
table._argH[j] = 20 # Set cell height for wrapping text
# Add table to the elements list
elements.append(Paragraph(title, normal_style))
elements.append(table)
elements.append(Paragraph("", normal_style)) # Add empty line for spacing
# Build the PDF document
doc.build(elements)
# Example usage:
# Create example DataFrames, titles, and calculated prices
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'X': [7, 8, 9], 'Y': [10, 11, 12]})
df3 = pd.DataFrame({'M': [13, 14, 15], 'N': [16, 17, 18]})
df_list = [df1, df2, df3]
titles = ['DataFrame 1', 'DataFrame 2', 'DataFrame 3']
calculated_prices = [100, 200, 300] # Example calculated prices
# Generate PDF
generate_pdf2(df_list, titles, calculated_prices)
我收到以下错误:
<ipython-input-13-20731d70741e> in generate_pdf2(df_list, titles, calculated_prices)
105 for i in range(len(data)):
106 for j in range(len(data[i])):
--> 107 table._argW[i][j] = col_widths[j] # Set cell widths
108 table._argH[j] = 20 # Set cell height for wrapping text
109
TypeError: 'float' object does not support item assignment
也许这很简单,但我很挣扎。也许另一双眼睛可能会有所帮助。
您正在尝试为浮点对象赋值:
table._argW[i][j] = col_widths[j]
。 table._argW[i]
是一个浮点数,而不是一个列表,在 Python 中你不能这样做。
这是更正后的代码:
width, height = A4
for i in range(len(data[0])):
table._argW[i] = col_widths[i]
for i in range(len(data)):
table._argH[i] = 20