美文网首页
python获取和写入word内容和样式

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

作者: 慢半帧 | 来源:发表于2019-01-04 22:59 被阅读0次

获取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内容和样式

      本文链接:https://www.haomeiwen.com/subject/xpslrqtx.html