"""仅整理本例生成的 Word 样式和通用元数据；不改回归表单元格或数值。
由 README 的 Stata 导出步骤之后调用，去掉默认 Title 样式的装饰线。
"""
import argparse,os,tempfile,zipfile,xml.etree.ElementTree as ET
from pathlib import Path
p=argparse.ArgumentParser();p.add_argument('document',nargs='?',default='regression-table.docx');args=p.parse_args();target=Path(args.document)
ns={'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
with zipfile.ZipFile(target) as source:
 entries={entry.filename:(entry,source.read(entry.filename)) for entry in source.infolist()}
for name in ['word/document.xml','word/styles.xml']:
 root=ET.fromstring(entries[name][1])
 for parent in root.iter():
  for child in list(parent):
   if child.tag=='{'+ns['w']+'}pBdr':parent.remove(child)
 entry,_=entries[name];entries[name]=(entry,ET.tostring(root,encoding='utf-8',xml_declaration=True))
# 作者信息使用本例发布主体；不传播维护者本机的 Office 用户名或修订身份。
if 'docProps/core.xml' in entries:
 root=ET.fromstring(entries['docProps/core.xml'][1])
 for child in root:
  if child.tag.rsplit('}',1)[-1] in ('creator','lastModifiedBy'):child.text='Marx Max'
 entry,_=entries['docProps/core.xml'];entries['docProps/core.xml']=(entry,ET.tostring(root,encoding='utf-8',xml_declaration=True))
fd,name=tempfile.mkstemp(prefix='word-style-',suffix='.docx',dir=target.parent);os.close(fd)
try:
 with zipfile.ZipFile(name,'w',zipfile.ZIP_DEFLATED) as out:
  for entry,body in entries.values():out.writestr(entry,body)
 os.replace(name,target)
finally:
 if os.path.exists(name):os.unlink(name)
print('WORD_STYLE_COMPLETED')
