Basic Usage
A Basic Example
Write title, a paragraph and a signature row.
from poi import Sheet, Row, Cell
sheet = Sheet(
root=
[
Row(
children=[
Cell(
"title",
grow=True,
align="center",
)
]
),
Row(
children=[
Cell(
"this is your awesome content",
grow=True,
border=1,
valign="vcenter",
rowspan=4,
height=100,
)
]
),
Row(colspan=8, children=[
Cell("signature:", offset=5, grow=True, bg_color='yellow'),
Cell("")
],
)
],
)
sheet.write('out.xlsx')
Export A Data Table
from typing import NamedTuple
from datetime import datetime
import random
from poi import Sheet, Table
class Product(NamedTuple):
name: str
desc: str
price: int
created_at: datetime
img: str
data = [
Product(
name=f"prod {i}",
desc=f"desc {i}",
price=random.randint(1, 100),
created_at=datetime.now(),
img="./docs/assets/product.jpg",
)
for i in range(5)
]
columns = [
{
"type": "image",
"attr": "img",
"title": "Product Image",
"options": {"x_scale": 0.27, "y_scale": 0.25},
},
("name", "Name"),
("desc", "Description"),
("price", "Price"),
("created_at", "Create Time"),
]
sheet = Sheet(
root=Table(
data=data,
columns=columns,
row_height=80,
cell_style={
"color: red": lambda record, col: col.attr == "price" and record.price > 50
},
date_format="yyyy-mm-dd",
align="center",
border=1,
)
)
sheet.write("table.xlsx")
Use with your favorite Web Framework
You can integrate Poi with any web framework easily.
Flask
from flask import send_file
def export(sheet, filename):
return send_file(
sheet.to_bytes_io(),
attachment_filename=filename,
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)