在 Python 中将 PDF 表格与数据源集成
Contents
[
Hide
]
从 DataFrame 创建 PDF
这 create_pdf_from_dataframe 函数构建一个新的 PDF 并插入一个由 pandas DataFrame 生成的表格。这种方法对于数据已经以表格形式存在的报告工作流非常有用。
该函数执行以下步骤:
- 使用创建一个空的 PDF 文档
ap.Document(). - 向文档添加一个页面。
- 通过调用将 DataFrame 转换为 Aspose.PDF 表格
create_table_from_dataframe(df, max_rows). - 使用将表格添加到页面
page.paragraphs.add(table). - 将 PDF 保存到输出路径。
from os import path
import sys
import pandas as pd
import aspose.pdf as ap
from config import set_license, initialize_data_dir
def create_pdf_from_dataframe(
outfile: str, df: pd.DataFrame, max_rows: int = 20
) -> None:
# Create new PDF document
document = ap.Document()
page = document.pages.add()
table = create_table_from_dataframe(df, max_rows)
# Add table object to first page of input document
page.paragraphs.add(table)
document.save(outfile)
从 DataFrame 创建表
这 create_table_from_dataframe 函数将 DataFrame 转换为 Aspose.PDF Table 可以添加到任何页面的对象。
它执行以下操作:
- 创建一个空的
ap.Table()实例。 - 设置表格和单元格边框,以实现一致的格式。
- 使用 DataFrame 列名添加标题行。
- 从…添加数据行
df.head(max_rows). - 返回填充好的表格对象。
from os import path
import sys
import pandas as pd
import aspose.pdf as ap
from config import set_license, initialize_data_dir
def create_table_from_dataframe(df: pd.DataFrame, max_rows: int = 20) -> ap.Table:
"""Create an Aspose.PDF table from a pandas DataFrame."""
# Initializes a new instance of the Table
table = ap.Table()
# Set the table border color as LightGray
table.border = ap.BorderInfo(ap.BorderSide.ALL, 1, ap.Color.light_gray)
# Set the border for table cells
table.default_cell_border = ap.BorderInfo(
ap.BorderSide.BOTTOM, 1, ap.Color.light_gray
)
# Add header row with column names
header_row = table.rows.add()
header_row.is_row_broken = False # Prevent header row from being split across pages
for column_name in df.columns:
cell = header_row.cells.add(str(column_name))
cell.background_color = ap.Color.light_gray
# Add data rows
for row_data in df.head(max_rows).itertuples(index=False):
row = table.rows.add()
for value in row_data:
row.cells.add(str(value))
return table