import textwrap class HTMLFormatter(object): STYLE = textwrap.dedent("""\ body { color: black; font-size: 18px; font-family: Arial, sans-serif; } .ex { display: inline; text-align: right; padding: 3px; margin: 24px; float: left; } .op { padding-right: 18px; } .eq { min-width: 48px; border-bottom: 1px solid black; } """) def __init__(self, style=''): self.style = style if style != '' else self.STYLE def format(self, sheet): gen = [self._format_header(sheet.seed)] for (f0, f1), answer in sheet.entries: gen.append(self._format_entry(f0, f1, answer)) gen.append(self._format_footer()) return '\n'.join(gen) def _format_header(self, seed): return '\n'.join([ '', '', '
', '', '{}'.format(f0), '
', '', 'x', '{}'.format(f1), '
', '', '
', ' '.format(answer), '