python获取和写入word内容和样式

获取word内容和样式

import docx
file = docx.Document(path)
  # 读取word
  for p in file.paragraphs:
      print(p.text)    # 打印出word中每段的内容
      print(p.style.name)    # 打印出word中每段的样式名称

写入word内容和样式

import docx
document = docx.Document()
yiwen = models.yiwen.objects.filter(file = file)
for y in yiwen:
    print(y.yiwen)
    print(y.yiwen_style)
    if len(y.yiwen_style) > 0:
        document.add_paragraph(y.yiwen, style=y.yiwen_style)
    else:
        document.add_paragraph(y.yiwen)
    # 路径
    path = "E:\\Man-Machine Translation System\\second_Edition\\static\\work\\yiwen_doc\\word.docx"
    # 保存
    document.save(path)

你可能感兴趣的:(python获取和写入word内容和样式)