使用office,wps工具打开文件另存为python
针对doc文件,antiword 提取doc文件信息linux
2.1 安装windows
windows工具
一、 下载zip包 地址:http://www.winfield.demon.nl/ 二、 解压至指定目录,配置系统环境变量path 三、 使用antiword命令行操做doc文件
Linux命令行
一、 wget http://www.winfield.demon.nl/linux/antiword-0.37.tar.gz 二、 tar -zxvf antiword-0.37.tar.gz 三、 cd antiword-0.37 四、 make && make install
2.2 使用说明code
针对docx文件,使用python-docx包ip
def trans_docx_txt(path): ''' fun: docx 文件转txt文件 ''' newpath = "".join(path.split(".")[:-1]) + ".txt" # 保存的文件若是存在须要先删除 if os.path.exists(newpath): os.remove(newpath) # 空文件不处理 if os.path.getsize(path) == 0: return file = docx.Document(path) # 读取段落 for paragraph in file.paragraphs: if paragraph.text: with open(newpath, "a+", encoding="utf-8") as file: file.write(paragraph.text + "\n") file = docx.Document(path) # 若是存在表格,读取表中单元格信息 if file.tables: for table in file.tables: row_count = len(table.rows) colu_count = len(table.columns) for i in range(row_count): for j in range(colu_count): with open(newpath, "a+", encoding="utf-8") as file: file.write(table.cell(i, j).text + "\n")