好的,所以这就是我想要做的。我想轻松地将文本从文本文件转换为word文档。我现在有这个......
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx')
print("Document saved.")
在读取文本文件中的文本的同时,每个单词都会添加到列表中。然后所有单词都被添加到Document
但问题是所有单词一起运行并且没有任何空格。
以下是文字的示例......
GetreadytoentertheThrivetimeshowontalk.Radio1170broadcastinglivefromthecenteroftheuniverse.It'SbusinessschoolwithouttheBSfeaturingoptometristturnedentrepreneur.Dr.RobertzoellnerwithusSBA,entrepreneuroftheYearclayClark.Dowehavecominginfromoneofourlistenersthattheyasked?Howcanyoucontrolemployeesthatyoucannotfire?HowcanyoucontrolemployeesthatyoucannotfirewellSteve?Couldyouthrowoutsomeinstanceswherethatcouldbeathingwhereyoucouldn'tfiretosuchasuper?
所以我想知道这是最好的方法吗?有更简单的方法吗?任何帮助将非常感激。先感谢您!!!
答案 0 :(得分:5)
为什么你把这句话分成几句? 如果要复制所有内容,则应该使用该行(将复制空格和换行符)而不是拆分它。 所以你的代码将是:
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
fileContents += line
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx')
print("Document saved.")
很好的评论btw!
快乐的编码!
答案 1 :(得分:2)
您可以使用" ".join(fileContents)
,因此您需要修改添加段落部分,如下所示:
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(" ".join(fileContents))
答案 2 :(得分:2)
为什么你要分裂空间并不清楚。如果您删除row = line.split(' ')
并制作后续行fileContents += line
,您会得到您想要的内容吗?您也可以按照前一个fileContents += '\n'
恢复换行符。