Открытие списка адресов файлов .docx - PullRequest
0 голосов
/ 16 июня 2019

Итак, у меня есть древовидная иерархия папок. Корневая папка с подкаталогами и другими подкаталогами, и в итоге у них есть файл .docx, к которому мне нужен доступ.

До сих пор мне удалось собрать адреса всех файлов .docx в списке. Я пытался выполнить операции (прочитайте файл .docx через python-docx и создайте из них csv), я хотел напрямую, но это не сработало. Так есть ли способ, которым я могу читать адреса из списка один за другим, открывать и делать то, что я хочу?

from docx import Document
import os

def getListOfFiles(dirName):
    # create a list of file and sub directories
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this 
directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles

dirName = r'path' ;
# Get the list of all files in directory tree at given path
listOfFiles = getListOfFiles(dirName)

print(len(listOfFiles))
for i in listOfFiles:
print(i)

#     document = Document('i.docx')
#     for p in document.paragraphs:
#         print("---------------")
#         print (p.text)
#         print("----------------")
...