Python 修复从.doc转换为.txt的表格格式

Python 修复从.doc转换为.txt的表格格式,python,text,doc,Python,Text,Doc,我正在将.doc文件转换为txt文件。 我唯一的问题是.doc文件包含表。 在txt文件中,这些表不是逐行写入的。 相反,它们是逐单元写入的 例如: 此表应为txt文件,如下所示: 术语和定义 狗有四条腿 猫很喜欢睡觉 熊喜欢蜂蜜 但实际上是这样的: 术语和定义 狗 他有四条腿 猫 他喜欢睡觉 忍受 我喜欢蜂蜜 这发生在my.doc文件中的每个表中 我的代码是: def doc_to_txt(path_of_file,name_of_file): try : app =

我正在将.doc文件转换为txt文件。 我唯一的问题是.doc文件包含表。 在txt文件中,这些表不是逐行写入的。 相反,它们是逐单元写入的

例如:

此表应为txt文件,如下所示:

术语和定义

狗有四条腿

猫很喜欢睡觉

熊喜欢蜂蜜

但实际上是这样的:

术语和定义

他有四条腿

他喜欢睡觉

忍受

我喜欢蜂蜜

这发生在my.doc文件中的每个表中

我的代码是:

def doc_to_txt(path_of_file,name_of_file):
    try :
        app = win32com.client.Dispatch('Word.Application')
        app.Visible = True
        for subdir, dirs, files in os.walk(path_of_file) :
            for file in files :
                fullpath = os.path.join(*[subdir, file])
                if file== name_of_file :
                        out_name = file.replace("doc", r"txt")
                        in_file = os.path.abspath(path_of_file + "\\" + file)
                        out_file = os.path.abspath(path_of_file + "\\" + out_name)
                        doc = app.Documents.Open(in_file)
                        content = doc.Content.Text
                        print('Converting '+name_of_file+' into txt file for parsing... ' )
                        #print('Exporting', out_file)
                        doc.SaveAs(out_file, FileFormat=7)
                        doc.Close()
                        return out_file
    except Exception as e :
        print(e)
    finally :
        app.Quit()

你能发布示例文档文件吗?@Alderven很抱歉,我无法上传文档文件,因为它来自我的工作。这就是为什么我删除了正常的表格,并在图片上写了随机的东西。