How to read pdf or pptx or docx file from ADLS gen2 in python using Synapse?

WBOY
Release: 2024-02-10 10:54:13
forward
566 people have browsed it

如何使用 Synapse 从 ADLS gen2 读取 python 中的 pdf 或 pptx 或 docx 文件?

Question content

I want to use python in synapse notebook to read files in different formats. These include .pdf, .pptx, .docx, .msg, and .eml. I want to be able to read files and then parse and manipulate them with python. I was able to do this in data blocks using different python libraries.

This is how I accomplish this task in data bricks:

from pptx import Presentation
prs = Presentation(file_name)

# for pdf
from pypdf import PdfReader
reader = PdfReader(open(filename, 'rb'))

# word docs
import docx
doc = docx.Document(file_name)

# .eml files
import email
msg = email.message_from_file(open(file_name))type here

# .msg files
import extract_msg
msg = extract_msg.Message(file_name)
Copy after login

In synapse I get the error: FileNotFoundError: [errno 2] No such file or directory.

These file paths can read csv, excel or txt data using spark or pandas, so I don't think there is an authorization or connection issue. The format is: abfs[s]://file_system_name@account_name.dfs.core.windows.net/file_path

I also tried mounting the storage location. This does help reading text files, but not other formats. Mounting the storage location in synapse


Correct answer


Install is the correct way, This answer explains it. I'm using synapse studio. The key is to use the file format obtained from the path command to the mounted storage. Otherwise I can basically use what I mentioned previously in the question. Only for pdf I had to change from using pypdf library to pypdf2.

The valid format is:

path = mssparkutils.fs.getmountpath("/mounted_name") 
# this gave me this format '/synfs/{jobid}/mounted_path/{filename}'
Copy after login

Format obtained from mssparkutils fs does not work

mssparkutils.fs.ls("synfs:/{jobId}/mounted_path/") 
# this gave a different format which did not work   'synfs:/{jobId}/mounted_path/{filename}'
Copy after login

The above is the detailed content of How to read pdf or pptx or docx file from ADLS gen2 in python using Synapse?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!