add table to output

This commit is contained in:
Maxim Slipenko 2021-09-18 19:45:25 +03:00
parent 46e07d3988
commit bae25e220a
3 changed files with 59 additions and 11 deletions

View File

@ -28,7 +28,7 @@ def main():
tm.step()
data.append(tm.info())
to_docx(args.output_file, data)
to_docx(args.output_file, program, data)
if __name__ == '__main__':
main()

View File

@ -59,16 +59,19 @@ def from_file(filename: str):
if symbol == " ":
symbol = "λ"
program[symbol] = []
program[symbol] = dict()
for i, action in enumerate(actions):
if not action:
continue
for action in actions:
new_symbol, direction, new_state = list(action)
if new_symbol == "_":
new_symbol = 'λ'
new_state = int(new_state) - 1
program[symbol].append([new_symbol, direction, new_state])
program[symbol][i] = ([new_symbol, direction, new_state])
f.close()

View File

@ -3,21 +3,66 @@ from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.shared import Pt
from docx.enum.text import WD_LINE_SPACING
from docx.enum.text import WD_ALIGN_PARAGRAPH
def to_docx(filename, data):
def add_state(p, state):
p.add_run('q')
index_text = p.add_run(str('z' if state == -1 else state))
index_text.font.subscript = True
def to_docx(filename, program, data):
document = Document()
section = document.sections[0]
sectPr = section._sectPr
cols = sectPr.xpath('./w:cols')[0]
cols.set(qn('w:num'),'2')
style = document.styles['Normal']
font = style.font
font.name = 'Times New Roman'
font.size = Pt(14)
rows = 0
cols = len(program.keys())
for x in program.keys():
rows = max(list(program[x].keys())[-1] + 1, rows)
table = document.add_table(rows + 1, cols + 1)
table.style = 'Table Grid'
table.autofit = True
table.allow_autofit = True
for i in range(1, rows + 1):
p = table.rows[i].cells[0].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
add_state(p, i)
for i, x in enumerate(program.keys()):
p = table.rows[0].cells[i + 1].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
p.add_run(x)
for j, (value) in enumerate(program.values()):
for i, (state, action) in enumerate(value.items()):
p = table.rows[state + 1].cells[j + 1].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
symbol, direction, state = action
add_state(p, state)
p.add_run(symbol)
p.add_run('R' if direction == '>' else 'L')
p = document.add_paragraph()
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
for i in range(len(data)):
p = document.add_paragraph('K')