簡體   English   中英

Python 在文本文件中寫入巨大的字符串,每 240 個字符換行一次

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

我需要將 word 文檔轉換為 html 代碼,然后將其保存到行數不超過 100 個字符的 .txt 文件中(如果它們不分開,稍后會有一個過程不會提取超過 255 個字符行)。

到目前為止,我已經成功(盡管歡迎更好的解決方案)設法將 .docx 文件轉換為 html 並將該變量部署到 a.txt 文件中。 但是,我無法弄清楚如何分隔線。 是否有任何集成的 function 可以實現這一目標?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM