Python reads a picture from Excel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.