Home  >  Article  >  Backend Development  >  How to read pdf or pptx or docx file from ADLS gen2 in python using Synapse?

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

WBOY
WBOYforward
2024-02-10 10:54:13566browse

如何使用 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)

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}'

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}'

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete