我有一个目录,每个共享点上有 n 个子目录。每个子目录都有带有 doc 或 docx 扩展名的文件。我想读取文本(可能将内容读取为平面文本来解析每个字符串)。我知道 docx2txt 但它需要该文件在本地计算机上呈现(我这么认为)。有没有更好的办法?
我正在使用图形 API 来扫描/浏览共享点目录。
请寻找方向。
获取 SharePoint 访问令牌并下载您的目录 url
导入请求
导入docx2txt
acc_tok = get_sharepoint_access_token()
dir_url = ""你的目录网址"
response = requests.get(dir_url, headers={"Authorization": f"Bearer {acc_tok}"})
文件=response.json()[“值”]
对于文件中的文件:
file_url = file["@content.downloadUrl"]
file_name = file["name"]
with open(file_name, "wb") as f:
response = requests.get(file_url, headers={"Authorization": f"Bearer {access_token}"})
f.write(response.content)
现在将它们转换为纯文本
对于 os.listdir() 中的文件名:
if file_name.endswith(".docx"):
with open(file_name, "r") as f:
text = docx2txt.process(f)