使用python将文件夹下的每个word放在excel表里
时间: 2024-05-07 19:17:17 浏览: 109
您好,我是 ChitGPT。针对您的问题,您可以使用 Python 中的 pandas 库来实现将文件夹下的每个 word 放在 Excel 表中。
您可以按照以下步骤进行操作:
1. 导入 pandas 库:
```
import pandas as pd
```
2. 打开文件夹并获取文件列表:
```
import os
path = '<文件夹路径>'
file_list = os.listdir(path)
```
3. 遍历文件列表并将每个 word 放入 pandas 的 DataFrame 中:
```
df = pd.DataFrame(columns=['word'])
for file_name in file_list:
if file_name.endswith('.docx'):
# 读取 word 文件中的内容
with open(os.path.join(path, file_name), 'rb') as file:
doc = Document(file)
text = ''
for paragraph in doc.paragraphs:
text += paragraph.text + '\n'
# 将 word 中的单词写入 DataFrame
words = text.split()
for word in words:
df = df.append({'word': word}, ignore_index=True)
```
4. 将 DataFrame 中的数据写入 Excel 表中:
```
writer = pd.ExcelWriter('<Excel 文件路径>')
df.to_excel(writer, index=False)
writer.save()
```
以上就是使用 Python 将文件夹下的每个 word 放在 Excel 表中的步骤,希望能够帮助到您。
阅读全文
相关推荐











