我仍然是python的新手,我正在制作一个简单的应用程序,它是从ppt文件中提取文本。
我有一个项目结构。
> Project_Python
>> Files
>>> Class A
- History.ppt
>>> Class B
- Animals.ppt
>> Result
???
- main.py我的问题是,如何读取Class A和Class B?的sub_folder中的文件,并希望它在打印后自动创建Result中的Files文件夹结构
这就是我尝试过的
from pptx import Presentation
import glob
import pathlib
import os
p_temp = pathlib.Path('Files') //How can I read sub folders folder dynamically
for eachfile in glob.glob("**/*.pptx"):
prs = Presentation(eachfile)
print(eachfile)
print("----------------------")
textdata = []
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
textdata.append(shape.text)
print(''.join(textdata[1:]) , file=open("Result/"+eachfile+".txt" , "a")) //Create the same folder structure of Files发布于 2020-05-27 15:14:54
除了使用glob.glob之外,您的代码几乎都是正确的。
您还应该传递recursive=True参数
要创建包含子目录的目录,可以使用os.makedirs
from pptx import Presentation
import glob
import pathlib
import os
p_temp = pathlib.Path('Files') //How can I read sub folders folder dynamically
for eachfile in glob.glob(p_temp+"**/*.pptx", recursive=True):
prs = Presentation(eachfile)
print(eachfile)
print("----------------------")
textdata = []
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
textdata.append(shape.text)
os.makedirs(str(pathlib.Path(eachfile).parent).replace('Files','Result')
print(''.join(textdata[1:]) , file=open("Result/"+eachfile+".txt" , "a")) //Create the same folder structure of Fileshttps://stackoverflow.com/questions/62037105
复制相似问题