用python循环遍历一个文件夹下的所有word文件,并将文件中的时间提取出来存入一个范围当中,并显示个数
时间: 2024-02-27 13:59:19 浏览: 104
好的,我来给您解答。
您可以使用Python内置模块os和docx来实现此功能。具体步骤如下:
1. 导入所需模块
```python
import os
import docx
```
2. 定义一个函数来遍历文件夹并提取文件中的时间
```python
def extract_time_from_docx(folder_path):
time_range = []
count = 0
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.docx'):
file_path = os.path.join(root, file)
doc = docx.Document(file_path)
for paragraph in doc.paragraphs:
if '时间' in paragraph.text:
time_str = paragraph.text.split('时间:')[1]
# 假设时间格式为yyyy-mm-dd
time_range.append(time_str)
count += 1
return time_range, count
```
3. 调用函数并输出结果
```python
folder_path = 'your/folder/path'
time_range, count = extract_time_from_docx(folder_path)
print('时间范围:', time_range)
print('时间数量:', count)
```
注意:在使用此函数前,需要确保您已经安装了docx模块。可以使用以下命令安装:
```python
pip install python-docx
```
阅读全文
相关推荐

















