使用python從docx中抽取特定段落並保存到txt文檔中


https://blog.csdn.net/HUSTER_LC/article/details/79367286

 

1、遇到問題

    工作中遇到一個問題,需要從dcox文檔中抽取特定的段落;通過對目標對象的調查,發現目標段落的公共特性:具有同樣的段落樣式,並且有共同的開頭Sysname;

    同時存在另外一個問題,存在多個目標文檔,且這些目標文檔存在同一個目標文件夾中

2、解決方案

    先解決問題1:獲取指定路勁下的特定文檔的目標段落

    在解決問題2 :獲取指定路徑下的docx文檔的列表

    1、問題 1 :

        使用python docx 獲取目標文檔的目標段落並使用re模塊查找包含Syname的段落

    2、問題 2:

        使用os改變工作路徑,並獲取特定路勁下的文檔列表,送給1進行處理

3、實施

    1、打開目標文檔,獲取目標段落

        

  1.  
    #-*- coding = utf-8 -*-
  2.  
    import docx
  3.  
    #獲取docx文檔的所有段落 path : 相對路徑包含文檔名稱
  4.  
    def getpara(path):
  5.  
         try :
  6.  
            docx_temp = docx.Document(path)
  7.  
         except :
  8.  
            print( "can't open the docx")
  9.  
             return False
  10.  
         try :
  11.  
            docx_para = docx_temp.paragraphs
  12.  
            print( "Succeed getting the para:",path)
  13.  
             return docx_para
  14.  
         except :
  15.  
            print( "can't get the ",path," paragraphs")
  16.  
             return False
  1.  
    import re
  2.  
    #從段落中抽取目標段落
  3.  
    def findpara(parpas,str = "Sysname"):
  4.  
    try :
  5.  
        para_list = [ "start"]
  6.  
        pattern = re.complie(str)
  7.  
         for para in paras :
  8.  
            match1 = pattern.search(para.text)
  9.  
             if match1 :
  10.  
                para_list.append(para.text)
  11.  
     
  12.  
        para_list.pop( 0)
  13.  
        retuen para_list
  14.  
    except :
  15.  
         return False

     2、將查找到的段落寫入txt文件

  1.  
    #將制定一個列表的內容寫入txt文件
  2.  
    def list2txt(list,name="com") :        #文件名默認為com.txt
  3.  
         if len(list) :
  4.  
         try :
  5.  
            fp = open( "com.txt","w")
  6.  
             for cloe in list :
  7.  
                fp.write(cloe)
  8.  
                fp.write( "\n")
  9.  
         except :
  10.  
             return False
  11.  
         finally:
  12.  
            fp.close()

    3、工作目錄切換與獲取指定路徑的文檔列表

 

  1.  
    import os
  2.  
    #切換工作路徑 返回該路徑下的文檔列表
  3.  
    def set_wd(wd == '0') :
  4.  
         if wd == '0' :
  5.  
             try :
  6.  
               os.chdir(wd)
  7.  
               File_List = os.listdir(wd)
  8.  
                return File_List
  9.  
                 except :
  10.  
                print( "Error")
  11.  
                return False
  12.  
         else :
  13.  
             try :
  14.  
               wd = os.getcwd()
  15.  
                os.chdir(wd)
  16.  
                print( "Using the current path word")
  17.  
                File_List = os.listdir(wd)
  18.  
                 return File_List
  19.  
             except :
  20.  
               print( "Error")
  21.  
                return False
  22.  
     
  23.  

    charles2843 7個月前 最后一段 def set_wd(wd == '0') : 這里在python 3.7環境下,語法不正確,應該是 def set_wd(wd='0'):


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM