-
-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I'm creating a plugin to export a spreadsheet file (.ods or .xlsx) #1310
Comments
ColinMaudry
changed the title
I'm creating a plugin to export a spreadsheet file (.ods or .xslx)
I'm creating a plugin to export a spreadsheet file (.ods or .xlsx)
Apr 28, 2021
Apparently, beside a string, Reponse could also work with bytes. |
I made it work with openpyxl. I'm not sure all the code under 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 I'll update this issue when the plugin is packaged and ready for broader use. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I have started developing a plugin to export records as a spreadsheet file. It could be ods or xlsx, whatever is easier.
I have spotted the following packages:
This is the code I have so far, I test it with the
--plugins-dir
option:I get the following error:
I tried with
AsgiFileDownload
like in DatabaseDownload to deal with the binary nature of the ods file, but the renderer expects a Response:However, the
Response
class only supports the following methods, not binary:How would you suggest me to proceed to have my ods file downloaded?
The text was updated successfully, but these errors were encountered: