讀取文本、圖、表、解壓信息
import docx import zipfile import os import shutil '''讀取word中的文本''' def gettxt(): file=docx.Document("gao.docx") print("段落數:"+str(len(file.paragraphs)))#段落數為13,每個回車隔離一段 #輸出每一段的內容 # for para in file.paragraphs: # print(para.text) #輸出段落編號及段落內容 for i in range(len(file.paragraphs)): if len(file.paragraphs[i].text.replace(' ',''))>4: print("第"+str(i)+"段的內容是:"+file.paragraphs[i].text) '''讀取word中的table''' def gettable(): doc = docx.Document('word.docx') for table in doc.tables: # 遍歷所有表格 print ('----table------') for row in table.rows: # 遍歷表格的所有行 # row_str = '\t'.join([cell.text for cell in row.cells]) # 一行數據 # print row_str for cell in row.cells: print (cell.text, '\t') '''獲取解壓后的文件信息''' def getinfo(wordfile): f=zipfile.ZipFile(wordfile,'r') for filename in f.namelist(): f.extract(filename) print(filename) ''' 輸出解壓后的信息: _rels/ _rels/.rels customXml/ customXml/_rels/ customXml/_rels/item1.xml.rels customXml/_rels/item2.xml.rels customXml/item1.xml customXml/item2.xml customXml/itemProps1.xml customXml/itemProps2.xml docProps/ docProps/app.xml docProps/core.xml docProps/custom.xml docProps/thumbnail.wmf word/ word/_rels/ word/_rels/document.xml.rels word/document.xml word/fontTable.xml word/media/ word/media/image1.jpeg word/numbering.xml word/settings.xml word/styles.xml word/theme/ word/theme/theme1.xml ''' ''' ------獲取圖: word文檔的路徑 zip壓縮文件的路徑 臨時解壓的tmp路徑 最后需要保存的store_path路徑 ''' def getpic(path, zip_path, tmp_path, store_path): ''' :param path:源文件 :param zip_path:docx重命名為zip :param tmp_path:中轉圖片文件夾 :param store_path:最后保存結果的文件夾(需要手動創建) :return: ''' '''=============將docx文件重命名為zip文件====================''' os.rename(path, zip_path) # 進行解壓 f = zipfile.ZipFile(zip_path, 'r') # 將圖片提取並保存 for file in f.namelist(): f.extract(file, tmp_path) # 釋放該zip文件 f.close() '''=============將docx文件從zip還原為docx====================''' os.rename(zip_path, path) # 得到緩存文件夾中圖片列表 pic = os.listdir(os.path.join(tmp_path, 'word/media')) '''=============將圖片復制到最終的文件夾中====================''' for i in pic: # 根據word的路徑生成圖片的名稱 new_name = path.replace('\\', '_') new_name = new_name.replace(':', '') + '_' + i shutil.copy(os.path.join(tmp_path + '/word/media', i), os.path.join(store_path, new_name)) '''=============刪除緩沖文件夾中的文件,用以存儲下一次的文件====================''' for i in os.listdir(tmp_path): # 如果是文件夾則刪除 if os.path.isdir(os.path.join(tmp_path, i)): shutil.rmtree(os.path.join(tmp_path, i)) if __name__ == '__main__': # 源文件 path = r'E:\dogcat\提取圖片\log.docx' # docx重命名為zip zip_path = r'E:\dogcat\提取圖片\log.zip' # 中轉圖片文件夾 tmp_path = r'E:\dogcat\提取圖片\tmp' # 最后保存結果的文件夾 store_path = r'E:\dogcat\提取圖片\測試' m = getpic(path, zip_path, tmp_path, store_path)
至於處理doc文件直接轉存成docx文件就可以了
def docTTTTTdocx(doc_name, docx_name):
try: # 首先將doc轉換成docx word = client.Dispatch("Word.Application") doc = word.Documents.Open(doc_name) # 使用參數16表示將doc轉換成docx doc.SaveAs(docx_name, 16) doc.Close() word.Quit() except: pass
這里如果轉換不成功,可能是路徑的問題,把doc_name換成完整路徑,如下:
from win32com.client import Dispatch
def docToDocxR(docPath, docxPath):
'''將doc轉存為docx'''
word = Dispatch('Word.Application')
pathPrefix = sys.path[0]+'\\'
print(pathPrefix)
doc = word.Documents.Open(pathPrefix+docPath)
doc.SaveAs(pathPrefix+docxPath, FileFormat=12)
doc.Close()
word.Quit()
參考: