Python reads the picture file from Excel and turns it into base64
ImportSYSImportOSImportxlrdImportZipFileImportBase64classExcelimgread (object):defChange_file_name (self, file_path, old_name, New_type ='. zip'): """Modify the file type name under the specified directory:p Aram File_path::p Aram old::p Aram New:: Return:"""Old_path=Os.path.join (File_path, Old_name)if notos.path.exists (old_path):Print('No such file!:%s'%Old_path)returnFalse new_name= Str (Old_name.split ('.') [0]) +New_type New_path=Os.path.join (File_path, new_name)ifos.path.exists (New_path): Os.remove (New_path) os.rename (Old_path, New_path)defunzip_file (Self, file_path):"""unzip the zip file under the specified directory:p Aram File_path:: Return:"""File_list =Os.listdir (File_path) forfile_nameinchfile_list:ifFile_name.split ('.') [1] = ='Zip': File_zip= ZipFile. ZipFile (Os.path.join (File_path, file_name),'R') Zipdir= File_name.split ('.') [0] forFilesinchfile_zip.namelist (): File_zip.extract (Files, Os.path.join (File_path, Zipdir))#unzip to the specified file directoryfile_zip.close ()defunzip_excel_pic2base64 (self, File_path, file_name):"""unzip the Excel directory to get the picture and turn it into Base64 encoding:p Aram File_path::p Aram file_name:: Return:"""Pic_dir='Xl\media'Pic_path= Os.path.join (File_path, File_name.split ('.') [0], Pic_dir)if notos.path.exists (pic_path):Print('No such directory!:%s'%Pic_path)return "'file_list=Os.listdir (Pic_path)Try: forFilesinchfile_list:ifFiles.split ('.') [1] = ='PNG':
= Os.path.join (pic_path, files) return staionreport (). Img2base64 (Path) # Turn to Base64 method except: print ('unzip_excel_pic2base64 error! ' ) return'
def Excel_pic_read (self, File_path, file_name): """ read the picture in Excel Base64 :p Aram File_path: :p Aram file_name: : Return: Image base64 encoded string """
Excelimgread
Excelimgread
Excelimgread(). Unzip_excel_pic2base64 (File_path, file_name)
Python reads a picture from Excel