docx 在 python 中列出

Kip*_*per 1 python python-2.7 python-docx

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

例子:

.docx 文件:

"Hello, my name is blabla,
I am 30 years old.
I have two kids."
Run Code Online (Sandbox Code Playgroud)

结果:

['Hello, my name is blabla', 'I am 30 years old', 'I have two kids']
Run Code Online (Sandbox Code Playgroud)

我无法让它工作。

使用docx2txt这里的模块: github链接

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

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

Din*_*kar 5

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)
Run Code Online (Sandbox Code Playgroud)

输出:

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>
Run Code Online (Sandbox Code Playgroud)