gpt4 book ai didi

python - docx 在 python 中列出

转载 作者:行者123 更新时间:2023-12-02 15:16:57 27 4
gpt4 key购买 nike

我正在尝试读取 docx 文件并将文本添加到列表中。现在我需要列表来包含 docx 文件中的行。

例子:

文档文件:

"Hello, my name is blabla,
I am 30 years old.
I have two kids."

结果:

['Hello, my name is blabla', 'I am 30 years old', 'I have two kids']

我无法让它工作。

使用此处的 docx2txt 模块: github link

进程只有一个命令,它返回docx文件中的所有文本。

我还希望它保留特殊字符,如 ":\-\.\,"

最佳答案

docx2txt模块读取docx文件并将其转换为文本格式。

您需要使用 splitlines() 拆分以上输出并将其存储在列表中。

代码(内联注释):

import docx2txt

text = docx2txt.process("a.docx")

#Prints output after converting
print ("After converting text is ",text)

content = []
for line in text.splitlines():
#This will ignore empty/blank lines.
if line != '':
#Append to list
content.append(line)

print (content)

输出:

C:\Users\dinesh_pundkar\Desktop>python c.py
After converting text is
Hello, my name is blabla.

I am 30 years old.

I have two kids.

List is ['Hello, my name is blabla.', 'I am 30 years old. ', 'I have two kids.']

C:\Users\dinesh_pundkar\Desktop>

关于python - docx 在 python 中列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39717217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com