"""Small valid, editable blank ODF/DOCX documents and inert previews.
No cloud APIs, external requests, macros or document execution.
"""
from html import escape
from io import BytesIO
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile, BadZipFile
import xml.etree.ElementTree as ET
ODF_TYPES = {
'odt': ('application/vnd.oasis.opendocument.text', 'text'),
'ods': ('application/vnd.oasis.opendocument.spreadsheet', 'spreadsheet'),
'odp': ('application/vnd.oasis.opendocument.presentation', 'presentation'),
}
BLANK_TYPES = {'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
**{ext: mime for ext, (mime, _) in ODF_TYPES.items()},
**{ext: 'text/plain; charset=utf-8' for ext in ('txt', 'md', 'py', 'js', 'jsx', 'ts', 'tsx', 'css', 'html', 'json', 'xml', 'csv')}}
ODF_NS = 'urn:oasis:names:tc:opendocument:xmlns:'
OFFICE = '{' + ODF_NS + 'office:1.0}'
TEXT = '{' + ODF_NS + 'text:1.0}'
TABLE = '{' + ODF_NS + 'table:1.0}'
DRAW = '{' + ODF_NS + 'drawing:1.0}'
MAX_DOC_BYTES = 35 * 1024 * 1024
MAX_CONTENT_BYTES = 12 * 1024 * 1024
def blank_file(extension):
if extension not in BLANK_TYPES:
raise ValueError('Format non autorisé.')
if extension in ODF_TYPES:
# Valid empty templates tested with LibreOffice, no server dependency.
return (Path(__file__).with_name('templates') / f'blank.{extension}').read_bytes()
if extension != 'docx':
return b'' if extension != 'json' else b'{}\n'
out = BytesIO()
with ZipFile(out, 'w', ZIP_DEFLATED) as archive:
archive.writestr('[Content_Types].xml', '''
| ' + cell_text + ' | '] * min(repeat, max(0, 60 - len(cells)))) if len(cells) >= 60: break if cells: sections.append('
' + x + '
' for x in paras) + 'Document vide
' return ('''