使用 python-docx
库,调整docx文件格式
- 设置页面大小
- 设置页边距
- 设置分栏数量
- 清理页眉页脚
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import docx from docx.shared import Mm from docx.oxml.ns import qn
doc = docx.Document('myfile.docx')
doc.settings.odd_and_even_pages_header_footer = False for section in doc.sections: section.page_width = Mm(210) section.page_height = Mm(297) section.top_margin = Mm(5) section.bottom_margin = Mm(5) section.left_margin = Mm(5) section.right_margin = Mm(5) sectPr = section._sectPr cols = sectPr.xpath('./w:cols')[0] cols.set(qn('w:num'),'1') section.different_first_page_header_footer = False section.header.is_linked_to_previous = True section.footer.is_linked_to_previous = True doc.save('myfile.docx')
|