Python 在文本文件中写入巨大的字符串,每 240 个字符换行一次

Python Writing huge string in text file with a new line character every 240 characters

我需要将一个word文档转换为html代码,然后将其保存到一个.txt文件中,每行不超过100个字符(后面有一个过程不会超过255 个字符,如果它们不在单独的行中)。

到目前为止,我已经成功(尽管欢迎更好的解决方案)设法将 .docx 文件转换为 html 并将该变量部署到 .txt 文件中。但是,我无法弄清楚如何分隔线条。有没有集成的功能可以实现这个?

import mammoth

with open(r'C:\Users\uXXXXXX\Downloads\Test_Script.docx', "rb") as docx_file:
    result = mammoth.convert_to_html(docx_file)
    html = result.value # The generated HTML
    messages = result.messages # Any messages, such as warnings during conversion
    
with open(r'C:\Users\uXXXXXX\Downloads\Output.txt', 'w') as text_file:
    text_file.write(html)

那样的话,你就可以

html = "..."
i = 100
while i < len(html):
    html = html[:i] + "\n" + html[i:]
    i += 101