from docx import Document
alphaDic = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!','?','.','~',',','(',')','$','-',':',';',"'",'/']
doc = Document('realexample.docx')
docIndex = 0
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None
while docIndex < len(doc.paragraphs):
firstSen = doc.paragraphs[docIndex].text
rep_dic = {ord(k):None for k in alphaDic + [x.upper() for x in alphaDic]}
translation = (firstSen.translate(rep_dic))
removeExcessSpaces = " ".join(translation.split())
if removeExcessSpaces != '':
doc.paragraphs[docIndex].text = removeExcessSpaces
else:
delete_paragraph(doc.paragraphs[docIndex])
docIndex -=1 # go one step back in the loop because of the deleted index
docIndex +=1因此,测试文档如下所示
Hello
你好
Good afternoon
朋友们
Good evening
晚上好我正在努力实现下面的结果。
你好
朋友们
晚上好现在,代码删除了所有的空段落和多余的空格,并做到了这一点,所以我有点卡在这里了。我只想删除由英语单词引起的换行。
你好
朋友们
晚上好发布于 2017-03-16 09:53:02
你可以做的就是查找英文单词,一旦你找到了英文单词" word ",在后面加上"\n“,然后从文档中删除这个新的结果"WORD\n”。在python中附加字符串的方式是使用+符号。只需执行"WORD“+”"\n“
https://stackoverflow.com/questions/42823364
复制相似问题