Export Images From Excel using Python with specific name












0















I am trying to read an excel using python, Excel has two columns name as Product_Name and second is LOGO. product Name as the name suggests contains the Product Name Like Fish ,Laptop whereas Second columns contain the logo of that Product Name.I am trying to save images from the LOGO column with image name as Product Name .Below Code is working fine but Product Name and saved Images are mismatching



import win32com.client       # Need pywin32 from pip
from PIL import ImageGrab # Need PIL as well
import os
excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.ActiveWorkbook
wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
print(wb_path)
print("Extracting images from %s" % wb_path)
image_no = 0
for sheet in workbook.Worksheets:
if(sheet.Name == "Ch"):
for shape,r in zip(sheet.Shapes,range(4,200)):
if shape.Name.startswith("Picture"):
image_no += 1
print("---- Image No. %07i ----" % image_no)
print(r)
imagen = sheet.Cells(r,'E').value
filename = sheet.Cells(r,'E').value + ".jpg"
file_path = os.path.join (wb_folder, filename)
print("Saving as %s" % file_path) # Debug output
shape.Copy() # Copies from Excel to Windows clipboard
# Use PIL (python imaging library) to save from Windows clipboard
# to a file
image = ImageGrab.grabclipboard()
print(image)
try:
image.save(file_path,'jpeg')
except AttributeError:
F = open('error.txt','w')
F.write(imagen)
F.close()









share|improve this question

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Jan 10 at 11:03
















0















I am trying to read an excel using python, Excel has two columns name as Product_Name and second is LOGO. product Name as the name suggests contains the Product Name Like Fish ,Laptop whereas Second columns contain the logo of that Product Name.I am trying to save images from the LOGO column with image name as Product Name .Below Code is working fine but Product Name and saved Images are mismatching



import win32com.client       # Need pywin32 from pip
from PIL import ImageGrab # Need PIL as well
import os
excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.ActiveWorkbook
wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
print(wb_path)
print("Extracting images from %s" % wb_path)
image_no = 0
for sheet in workbook.Worksheets:
if(sheet.Name == "Ch"):
for shape,r in zip(sheet.Shapes,range(4,200)):
if shape.Name.startswith("Picture"):
image_no += 1
print("---- Image No. %07i ----" % image_no)
print(r)
imagen = sheet.Cells(r,'E').value
filename = sheet.Cells(r,'E').value + ".jpg"
file_path = os.path.join (wb_folder, filename)
print("Saving as %s" % file_path) # Debug output
shape.Copy() # Copies from Excel to Windows clipboard
# Use PIL (python imaging library) to save from Windows clipboard
# to a file
image = ImageGrab.grabclipboard()
print(image)
try:
image.save(file_path,'jpeg')
except AttributeError:
F = open('error.txt','w')
F.write(imagen)
F.close()









share|improve this question

























  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Jan 10 at 11:03














0












0








0


1






I am trying to read an excel using python, Excel has two columns name as Product_Name and second is LOGO. product Name as the name suggests contains the Product Name Like Fish ,Laptop whereas Second columns contain the logo of that Product Name.I am trying to save images from the LOGO column with image name as Product Name .Below Code is working fine but Product Name and saved Images are mismatching



import win32com.client       # Need pywin32 from pip
from PIL import ImageGrab # Need PIL as well
import os
excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.ActiveWorkbook
wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
print(wb_path)
print("Extracting images from %s" % wb_path)
image_no = 0
for sheet in workbook.Worksheets:
if(sheet.Name == "Ch"):
for shape,r in zip(sheet.Shapes,range(4,200)):
if shape.Name.startswith("Picture"):
image_no += 1
print("---- Image No. %07i ----" % image_no)
print(r)
imagen = sheet.Cells(r,'E').value
filename = sheet.Cells(r,'E').value + ".jpg"
file_path = os.path.join (wb_folder, filename)
print("Saving as %s" % file_path) # Debug output
shape.Copy() # Copies from Excel to Windows clipboard
# Use PIL (python imaging library) to save from Windows clipboard
# to a file
image = ImageGrab.grabclipboard()
print(image)
try:
image.save(file_path,'jpeg')
except AttributeError:
F = open('error.txt','w')
F.write(imagen)
F.close()









share|improve this question
















I am trying to read an excel using python, Excel has two columns name as Product_Name and second is LOGO. product Name as the name suggests contains the Product Name Like Fish ,Laptop whereas Second columns contain the logo of that Product Name.I am trying to save images from the LOGO column with image name as Product Name .Below Code is working fine but Product Name and saved Images are mismatching



import win32com.client       # Need pywin32 from pip
from PIL import ImageGrab # Need PIL as well
import os
excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.ActiveWorkbook
wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
print(wb_path)
print("Extracting images from %s" % wb_path)
image_no = 0
for sheet in workbook.Worksheets:
if(sheet.Name == "Ch"):
for shape,r in zip(sheet.Shapes,range(4,200)):
if shape.Name.startswith("Picture"):
image_no += 1
print("---- Image No. %07i ----" % image_no)
print(r)
imagen = sheet.Cells(r,'E').value
filename = sheet.Cells(r,'E').value + ".jpg"
file_path = os.path.join (wb_folder, filename)
print("Saving as %s" % file_path) # Debug output
shape.Copy() # Copies from Excel to Windows clipboard
# Use PIL (python imaging library) to save from Windows clipboard
# to a file
image = ImageGrab.grabclipboard()
print(image)
try:
image.save(file_path,'jpeg')
except AttributeError:
F = open('error.txt','w')
F.write(imagen)
F.close()






python python-3.x image pywin32






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 14:25









stovfl

8,08831133




8,08831133










asked Jan 1 at 8:41









ashish gaashish ga

32




32













  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Jan 10 at 11:03



















  • Comments are not for extended discussion; this conversation has been moved to chat.

    – Samuel Liew
    Jan 10 at 11:03

















Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Jan 10 at 11:03





Comments are not for extended discussion; this conversation has been moved to chat.

– Samuel Liew
Jan 10 at 11:03












1 Answer
1






active

oldest

votes


















0














Following script extracts all images from Excel file and name them with "Channel name" value:



import re
from PIL import ImageGrab
import win32com.client as win32

FILE = r'C:UsersuserDesktopso53994108logo.xlsx'
CELLS = [(4, 5, 'F'), (3, 3, 'D')]

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(FILE)
for i, worksheet in enumerate(workbook.Sheets):
row = CELLS[i][0]
while True:
name = worksheet.Cells(row, CELLS[i][1]).Value
if not name:
break
name = re.sub(r'W+ *', ' ', name)
rng = worksheet.Range('{}{}'.format(CELLS[i][2], row))
rng.CopyPicture(1, 2)
im = ImageGrab.grabclipboard()
im.save('{}.jpg'.format(name))
row += 1


So I've got following images on the end:



enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994108%2fexport-images-from-excel-using-python-with-specific-name%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Following script extracts all images from Excel file and name them with "Channel name" value:



    import re
    from PIL import ImageGrab
    import win32com.client as win32

    FILE = r'C:UsersuserDesktopso53994108logo.xlsx'
    CELLS = [(4, 5, 'F'), (3, 3, 'D')]

    excel = win32.gencache.EnsureDispatch('Excel.Application')
    workbook = excel.Workbooks.Open(FILE)
    for i, worksheet in enumerate(workbook.Sheets):
    row = CELLS[i][0]
    while True:
    name = worksheet.Cells(row, CELLS[i][1]).Value
    if not name:
    break
    name = re.sub(r'W+ *', ' ', name)
    rng = worksheet.Range('{}{}'.format(CELLS[i][2], row))
    rng.CopyPicture(1, 2)
    im = ImageGrab.grabclipboard()
    im.save('{}.jpg'.format(name))
    row += 1


    So I've got following images on the end:



    enter image description here






    share|improve this answer




























      0














      Following script extracts all images from Excel file and name them with "Channel name" value:



      import re
      from PIL import ImageGrab
      import win32com.client as win32

      FILE = r'C:UsersuserDesktopso53994108logo.xlsx'
      CELLS = [(4, 5, 'F'), (3, 3, 'D')]

      excel = win32.gencache.EnsureDispatch('Excel.Application')
      workbook = excel.Workbooks.Open(FILE)
      for i, worksheet in enumerate(workbook.Sheets):
      row = CELLS[i][0]
      while True:
      name = worksheet.Cells(row, CELLS[i][1]).Value
      if not name:
      break
      name = re.sub(r'W+ *', ' ', name)
      rng = worksheet.Range('{}{}'.format(CELLS[i][2], row))
      rng.CopyPicture(1, 2)
      im = ImageGrab.grabclipboard()
      im.save('{}.jpg'.format(name))
      row += 1


      So I've got following images on the end:



      enter image description here






      share|improve this answer


























        0












        0








        0







        Following script extracts all images from Excel file and name them with "Channel name" value:



        import re
        from PIL import ImageGrab
        import win32com.client as win32

        FILE = r'C:UsersuserDesktopso53994108logo.xlsx'
        CELLS = [(4, 5, 'F'), (3, 3, 'D')]

        excel = win32.gencache.EnsureDispatch('Excel.Application')
        workbook = excel.Workbooks.Open(FILE)
        for i, worksheet in enumerate(workbook.Sheets):
        row = CELLS[i][0]
        while True:
        name = worksheet.Cells(row, CELLS[i][1]).Value
        if not name:
        break
        name = re.sub(r'W+ *', ' ', name)
        rng = worksheet.Range('{}{}'.format(CELLS[i][2], row))
        rng.CopyPicture(1, 2)
        im = ImageGrab.grabclipboard()
        im.save('{}.jpg'.format(name))
        row += 1


        So I've got following images on the end:



        enter image description here






        share|improve this answer













        Following script extracts all images from Excel file and name them with "Channel name" value:



        import re
        from PIL import ImageGrab
        import win32com.client as win32

        FILE = r'C:UsersuserDesktopso53994108logo.xlsx'
        CELLS = [(4, 5, 'F'), (3, 3, 'D')]

        excel = win32.gencache.EnsureDispatch('Excel.Application')
        workbook = excel.Workbooks.Open(FILE)
        for i, worksheet in enumerate(workbook.Sheets):
        row = CELLS[i][0]
        while True:
        name = worksheet.Cells(row, CELLS[i][1]).Value
        if not name:
        break
        name = re.sub(r'W+ *', ' ', name)
        rng = worksheet.Range('{}{}'.format(CELLS[i][2], row))
        rng.CopyPicture(1, 2)
        im = ImageGrab.grabclipboard()
        im.save('{}.jpg'.format(name))
        row += 1


        So I've got following images on the end:



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 9 at 8:55









        AldervenAlderven

        1,79221026




        1,79221026
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994108%2fexport-images-from-excel-using-python-with-specific-name%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter