我无法将输出字符串保存为d

2024-04-29 00:06:49 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,有人能帮我把这个字符串保存在文档文件里吗。问题是我不能保存输出字符串,即使我只保存最后一个字符串,但我得到的不是完整的字符串

 h='Hello everyone Im new to this world' 

def underline(x):
   print "\033[4m"+ x + "\033[0m"

for item in h.split():
if len(item) >= 5:
    print(underline(item))
else:
    print item

我希望输出保存在文档文件中


Tags: to字符串inhellonewforworlddef
1条回答
网友
1楼 · 发布于 2024-04-29 00:06:49

安装Python docx:

pip intall python-docx

然后使用以下代码:

from docx import Document
h='Hello everyone Im new to this world'
document = Document()
p = document.add_paragraph('')
for item in h.split():
    if len(item) >= 5:
        p.add_run(" "+item+" ").underline = True
    else:
        p.add_run(" "+item+" ")
document.save("test.docx")

相关问题 更多 >