home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 829885904

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions issue performed_via_github_app
https://github.com/simonw/datasette/issues/1310#issuecomment-829885904 https://api.github.com/repos/simonw/datasette/issues/1310 829885904 MDEyOklzc3VlQ29tbWVudDgyOTg4NTkwNA== 3747136 2021-04-30T06:58:46Z 2021-04-30T07:26:11Z NONE

I made it work with openpyxl. I'm not sure all the code under @hookimpl is necessary... but it works :)

```python from datasette import hookimpl from datasette.utils.asgi import Response from openpyxl import Workbook from openpyxl.writer.excel import save_virtual_workbook from openpyxl.cell import WriteOnlyCell from openpyxl.styles import Alignment, Font, PatternFill from tempfile import NamedTemporaryFile

def render_spreadsheet(rows): wb = Workbook(write_only=True) ws = wb.create_sheet() ws = wb.active ws.title = "decp"

columns = rows[0].keys()
headers = []
for col in columns :
    c = WriteOnlyCell(ws, col)
    c.fill = PatternFill("solid", fgColor="DDEFFF")
    headers.append(c)
ws.append(headers)

for row in rows:
    wsRow = []
    for col in columns:
        c = WriteOnlyCell(ws, row[col])
        if col == "objet" :
            c.alignment = Alignment(wrapText = True)
        wsRow.append(c)
    ws.append(wsRow)

with NamedTemporaryFile() as tmp:
    wb.save(tmp.name)
    tmp.seek(0)
    return Response(
        tmp.read(),
        headers={
            'Content-Disposition': 'attachment; filename=decp.xlsx',
            'Content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        }
    )

@hookimpl def register_output_renderer(): return {"extension": "xlsx", "render": render_spreadsheet, "can_render": lambda: False}

```

The key part was to find the right function to wrap the spreadsheet object wb. NamedTemporaryFile() did it!

I'll update this issue when the plugin is packaged and ready for broader use.

{
    "total_count": 1,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 1,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
870125126  
Powered by Datasette · Queries took 4.943ms · About: github-to-sqlite