import pandas as pdfrom reportlab.pdfgen import canvasdef excel_to_pdf(excel_path, pdf_path): # 读取Excel文件 df = pd.read_excel(excel_path) # 创建PDF文件 c = canvas.Canvas(pdf_path) # 遍历DataFrame的每一行 for i, row in df.iterrows(): # 将每一行的数据写入PDF for j, value in enumerate(row): c.drawString(72, 720 - (i * 24 + j * 14), str(value))
# 保存PDF文件 c.save()# 使用函数excel_path = 'path_to_your_excel_file.xlsx' # Excel文件的路径pdf_path = 'path_to_your_pdf_file.pdf' # 要生成的PDF文件的路径excel_to_pdf(excel_path, pdf_path)