将文本保存到docx文件python

问题描述 投票:0回答:2

我在python上有结构化的文本。我想将其保存到docx文件。

类似

text = "A simple text\n Structured"

with open('docx_file.docx', 'w') as f:
    f.write(text)
python docx
2个回答
0
投票

docx文件不是纯文本文件,因此,除非您不想为此使用库,否则我建议https://grokonez.com/python/how-to-read-write-word-docx-files-in-python-docx-module


0
投票

检查python-docx包:

from docx import Document

document = Document()
document.add_heading('A simple text', level=1)
document.add_paragraph('some more text ... ')

document.save('docx_file.docx)
© www.soinside.com 2019 - 2024. All rights reserved.