日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python rtf转txt_将DOC、RTF格式文件批量转为TXT格式文件

發布時間:2023/12/2 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python rtf转txt_将DOC、RTF格式文件批量转为TXT格式文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Windows 下將目錄下所有文件下的文件 拷貝到一個目錄中:搜索"."

安裝python 及 win32com模塊 pip install pypiwin32

創建LDA_RTF_TXT.py

from glob import glob

import shutil

import os

import warnings

warnings.filterwarnings(action='ignore',category=UserWarning,module='gensim')

import win32com

import win32con

import win32gui

import codecs

from win32com.client import Dispatch

import pythoncom

import os

import sys

dataset_loca=sys.argv[1]

dataset_loca3=sys.argv[2]+'/'

class MSOffice2txt():

def __init__(self, fileType=['doc','ppt']):

self.docCom = None

self.pptCom = None

pythoncom.CoInitialize()

if type(fileType) is not list:

return 'Error, please check the fileType, it must be list[]'

for ft in fileType:

if ft == 'doc':

self.docCom = self.docApplicationOpen()

def close(self):

self.docApplicationClose(self.docCom)

def docApplicationOpen(self):

docCom = win32com.client.Dispatch('Word.Application')

docCom.Visible = 1

docCom.DisplayAlerts = 0

docHwnd = win32gui.FindWindow(None, 'Microsoft Word')

win32gui.ShowWindow(docHwnd, win32con.SW_HIDE)

return docCom

def docApplicationClose(self,docCom):

if docCom is not None:

docCom.Quit()

def doc2Txt(self, docCom, docFile, txtFile):

doc = docCom.Documents.Open(FileName=docFile,ReadOnly=1)

doc.SaveAs(txtFile, 2)

doc.Close()

def translate(self, filename, txtFilename):

if filename.endswith('doc') or filename.endswith('docx'):

if self.docCom is None:

self.docCom = self.docApplicationOpen()

self.doc2Txt(self.docCom, filename, txtFilename)

return True

else:

return False

files = glob(dataset_loca)

oldfile=[]

count=0

msoffice = MSOffice2txt()

for file_name in files:

tmpp=file_name

tmpp=tmpp.split('\\',2)[-1]

tmpp=tmpp.split('.',1)[0]

tmpp=tmpp.replace(' ','_')

shutil.copyfile(file_name,dataset_loca3+tmpp+'.doc')

count=count+1

msoffice.translate(dataset_loca3+tmpp+'.doc', dataset_loca3+tmpp+'.txt')

在運行過程中,需要輸入兩個參數:第一個為轉換文件DOC、RTF的源目錄的絕對地址;第二個為轉換后的TXT格式文件存放的絕對地址。

轉換示例:

python LDA_RTF_TXT.py D:/2019/*.doc D:/2020

將D:/2019/*.doc路徑下的所有doc格式文件批量轉換為TXT文件存放至D:/2020路徑下。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python rtf转txt_将DOC、RTF格式文件批量转为TXT格式文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。