from pathlib import Path from docx import Document from docx.shared import Inches, Pt, RGBColor from docx.enum.text import WD_ALIGN_PARAGRAPH from docx.enum.table import WD_TABLE_ALIGNMENT, WD_CELL_VERTICAL_ALIGNMENT from docx.oxml import OxmlElement from docx.oxml.ns import qn import subprocess ROOT=Path(__file__).resolve().parents[1] ref=Document() sec=ref.sections[0] sec.page_width=Inches(8.5);sec.page_height=Inches(11) sec.top_margin=Inches(.78);sec.bottom_margin=Inches(.72) sec.left_margin=Inches(.9);sec.right_margin=Inches(.9) sec.header_distance=Inches(.28);sec.footer_distance=Inches(.3) normal=ref.styles['Normal'];normal.font.name='Liberation Serif';normal.font.size=Pt(11) normal.paragraph_format.line_spacing=1.13 normal.paragraph_format.space_after=Pt(7) normal.paragraph_format.widow_control=True for name in ['Body Text','First Paragraph']: try:s=ref.styles[name] except KeyError:s=ref.styles.add_style(name,1) s.base_style=normal;s.font.name='Liberation Serif';s.font.size=Pt(11) s.paragraph_format.line_spacing=1.13;s.paragraph_format.space_after=Pt(7) for i in range(1,4): s=ref.styles[f'Heading {i}'];s.font.name='Liberation Sans';s.font.size=Pt(16 if i==1 else 12) s.font.bold=True;s.font.color.rgb=RGBColor.from_string('16384A') s.paragraph_format.space_before=Pt(17 if i==1 else 11) s.paragraph_format.space_after=Pt(7) s.paragraph_format.keep_with_next=True ref.styles['Title'].font.name='Liberation Serif';ref.styles['Title'].font.size=Pt(27) ref.styles['Title'].font.color.rgb=RGBColor.from_string('16384A') ref.styles['Subtitle'].font.name='Liberation Sans';ref.styles['Subtitle'].font.size=Pt(12) ref.styles['Subtitle'].font.color.rgb=RGBColor.from_string('455A64') for name in ['Caption']: ref.styles[name].font.name='Liberation Sans';ref.styles[name].font.size=Pt(9) ref.styles[name].font.color.rgb=RGBColor.from_string('455A64') ref.save(ROOT/'analysis'/'reference.docx') subprocess.run(['pandoc',str(ROOT/'Research_Audit.md'),'-o',str(ROOT/'outputs'/'Machine_Consciousness_Mathematical_Audit.docx'), '--reference-doc',str(ROOT/'analysis'/'reference.docx'),'--resource-path',str(ROOT)],check=True) p=ROOT/'outputs'/'Machine_Consciousness_Mathematical_Audit.docx' doc=Document(p) # Header/footer, document metadata, small repeated table headers. for sec in doc.sections: hp=sec.header.paragraphs[0];hp.text='PERSISTENT ENDOGENOUS DIVERGENCE / MATHEMATICAL AUDIT' hp.style=doc.styles['Normal'];hp.paragraph_format.space_after=Pt(0) for r in hp.runs:r.font.name='Liberation Sans';r.font.size=Pt(8);r.font.color.rgb=RGBColor.from_string('455A64') f=sec.footer.paragraphs[0];f.alignment=WD_ALIGN_PARAGRAPH.RIGHT run=f.add_run('RESEARCH DRAFT ยท 17 SEP 2026 | ');run.font.name='Liberation Sans';run.font.size=Pt(8) field=OxmlElement('w:fldSimple');field.set(qn('w:instr'),'PAGE');f._p.append(field) for table_index,table in enumerate(doc.tables): table.autofit=False table.alignment=WD_TABLE_ALIGNMENT.CENTER table.style='Normal Table' widths={0:[1.65,2.35,2.7],1:[1.95,1.35,1.35,2.05],2:[2.2,4.5],3:[2.65,4.05],4:[1.45,5.25]}[table_index] tblpr=table._tbl.tblPr tw=tblpr.find(qn('w:tblW')) tw.set(qn('w:type'),'dxa');tw.set(qn('w:w'),str(round(6.7*1440))) for col,w in zip(table.columns,widths): col.width=Inches(w) margins=OxmlElement('w:tblCellMar') for edge,val in [('top',65),('bottom',65),('left',95),('right',95)]: el=OxmlElement('w:'+edge);el.set(qn('w:w'),str(val));el.set(qn('w:type'),'dxa');margins.append(el) tblpr.append(margins) for row in table.rows: for ci,cell in enumerate(row.cells): cell.width=Inches(widths[ci]) cell.vertical_alignment=WD_CELL_VERTICAL_ALIGNMENT.CENTER for para in cell.paragraphs: para.style=doc.styles['Normal'] para.paragraph_format.keep_with_next=False para.paragraph_format.keep_together=True para.paragraph_format.left_indent=Pt(0);para.paragraph_format.right_indent=Pt(0) para.paragraph_format.first_line_indent=Pt(0) for ri,row in enumerate(table.rows): trpr=row._tr.get_or_add_trPr() cant=OxmlElement('w:cantSplit');trpr.append(cant) if ri==0: for old in list(trpr.findall(qn('w:tblHeader'))): trpr.remove(old) h=OxmlElement('w:tblHeader');trpr.append(h) for c in row.cells: for para in c.paragraphs: para.paragraph_format.space_after=Pt(5);para.paragraph_format.space_before=Pt(5) para.paragraph_format.line_spacing=1.02 for run in para.runs:run.font.name='Liberation Sans';run.font.size=Pt(9) if ri==0: shade=OxmlElement('w:shd');shade.set(qn('w:fill'),'E9EFF2');c._tc.get_or_add_tcPr().append(shade) for para in c.paragraphs: for run in para.runs:run.bold=True # Keep each displayed equation together; Pandoc creates native Office Math. for para in doc.paragraphs: if para._p.findall('.//'+qn('m:oMathPara')): para.paragraph_format.keep_together=True para.paragraph_format.space_before=Pt(5);para.paragraph_format.space_after=Pt(8) # Compact the source ledger without changing the research body. ref_section=False for para in doc.paragraphs: if para.text=='References and source ledger': ref_section=True continue if ref_section: para.paragraph_format.line_spacing=1.02 para.paragraph_format.space_after=Pt(5) for run in para.runs: run.font.size=Pt(9) # Two figures get descriptive alt text. for i,el in enumerate(doc.element.body.iter(qn('wp:docPr'))): el.set('descr', 'Published context-effect contrasts with normal-approximation uncertainty.' if i==0 else 'Synthetic trajectories of the averaged scalar recurrence approaching the fixed point one.') doc.core_properties.title='Persistent Endogenous Divergence in Artificial Systems' doc.core_properties.subject='Mathematical audit, published-evidence review, and prospective protocol; no consciousness proof claimed' doc.core_properties.author='Technical audit prepared for Joseph W. Anady' doc.core_properties.keywords='machine consciousness; mathematical audit; alignment faking; reproducibility' doc.save(p) print(p)