Today in this tutorial we will discuss different tricks to extract images from Excel. Now you all must be thinking that what’s new in this to learn because it’s so easy to copy an image from the source then paste it into the paint application and save it.
But just think for a while what if you need to extract multiple images from Excel file?
Don’t you think this copying-pasting method is too much time-consuming for this task?
Thus to minimize your effort and time I have outlined some best tricks to extract pictures from Excel. So without wasting any more time let’s get into the blog.
To extract data from corrupt Excel file, we recommend this tool:
This software will prevent Excel workbook data such as BI data, financial reports & other analytical information from corruption and data loss. With this software you can rebuild corrupt Excel files and restore every single visual representation & dataset to its original, intact state in 3 easy steps:
- Download Excel File Repair Tool rated Excellent by Softpedia, Softonic & CNET.
- Select the corrupt Excel file (XLS, XLSX) & click Repair to initiate the repair process.
- Preview the repaired files and click Save File to save the files at desired location.
How To Extract Images From Excel?
1# Extract Images From Excel Worksheets Using Imageorprintoptions
2# Extract Multiple Images At Once From Excel Workbook
3# Extract The Actual Images From The Image URL Addresses
5# Extract Images From Excel Using Imageoptions Class And Getimages Methods
Let’s know how it is to be done….!
1# Extract Images From Excel Worksheets Using Imageorprintoptions
It is possible to extract images from Excel files and then save them to the local drive using the Imageorprintoptions.
Here is the sample code that you can use to extract images from Excel file and then save them.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Open a template Excel file
Workbook workbook = new Workbook(sourceDir + “sampleExtractImagesFromWorksheets.xlsx”);
// Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Get the first Picture in the first worksheet
Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[0];
// Set the output image file path
string picformat = pic.ImageType.ToString();
// Note: you may evaluate the image format before specifying the image path
// Define ImageOrPrintOptions
ImageOrPrintOptions printoption = new ImageOrPrintOptions();
// Specify the image format
printoption.ImageType = Drawing.ImageType.Jpeg;
// Save the image
pic.ToImage(outputDir + “outputExtractImagesFromWorksheets.jpg”, printoption);
To get detailed information on how to convert Excel workbook/worksheet to image file, check out this article.(https://docs.aspose.com/cells/java/render-the-worksheet-and-workbook-to-image-using-imageorprintoptions/)
2# Extract Multiple Images At Once From Excel Workbook
Another method that you can try to extract multiple images from Excel is by saving your workbook as a web page.
Using this method not only the images but also the shapes, objects, and charts are also gets saved into the folder.
Here are the steps that you need to follow:
- Open the workbook that contains images you are willing to save.
- Go to the File menu and hit the Save As option. This will open the Save As a window on your screen.
- Now you need to select the Excel file from which you want to extract the images.
- Hit the Save as type drop-down list and choose the Web Page format.
- Tap the save button, and then go to the specified folder.
Here you will there are two files present, first one is in the HTML file format and the second one is a folder that contains all the extracted charts pictures, objects, etc. of your workbook.
- Now you can simply drag the extracted images to some other folder as per your need.
You can also watch this informative video for more reference:
3# Extract The Actual Images From The Image URL Addresses
Use the following VBA code to quickly extract actual pictures from the image URL in the Excel workbook.
- Press the ALT + F11 This will open the VBA editor window.
- Go to the Insert tab and choose the Module option. Now paste the complete code given below in your opened Module Window.
VBA code: extract actual images from image URLs in Excel:
Sub URLPictureInsert()
Dim Pshp As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
Set Rng = ActiveSheet.Range(“A2:A5”)
For Each cell In Rng
filenam = cell
ActiveSheet.Pictures.Insert(filenam).Select
Set Pshp = Selection.ShapeRange.Item(1)
If Pshp Is Nothing Then GoTo lab
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
With Pshp
.LockAspectRatio = msoFalse
If .Width > xRg.Width Then .Width = xRg.Width * 2 / 3
If .Height > xRg.Height Then .Height = xRg.Height * 2 / 3
.Top = xRg.Top + (xRg.Height – .Height) / 2
.Left = xRg.Left + (xRg.Width – .Width) / 2
End With
lab:
Set Pshp = Nothing
Range(“A2”).Select
Next
Application.ScreenUpdating = True
End Sub
Code illustration:
- In the above-mentioned code, A2:A5 is the cell range having the URL addresses from which you want to extract the images. Well, you can change these cell references as per your need.
- Using the above code, you can also assign the size for your extracted images.
- Remember one thing that your extracted images will automatically get into the cell present next to the image URL column. You will get no option to save your extracted images to any specific cell.
- Basic knowledge of coding is important if in case of any character problem rendered.
- Press the F5 key for the execution of the above code. Within a few minutes, you will see that all your pictures will get extracted from the image URLs and they get saved in the column adjacent to the image URL.
4# Using The File Extension
In this method, file extensions play a very important role. So at first, you need to go to the folder in which you have kept your Excel file from which you need to extract the images.
- Go to the Organize > Folder> Search Options.
- Now click on the View and remove the checkmark from Hide extensions.
- Doing this will start displaying the entire file along with their extensions.
- Choose the Excel file from which you need to extract the image and then change the extension to “zip”.
- Your Excel file is now changed to a zip extractable file. Now make a right-click over it and hit the Extract files.
You will see two “Document.xml” files here. It is an XML file having all the text of the original word file. You can open it using the XML editor or simple notepad. You will also see a folder named “Media”. Just click it and you will see all the images of your original file here. The media folder saves the extracted images.
In this way, you can extract images from Excel files.
If in case your MS Office version fails to extract images with the above method. In that case, you can take the help of an Office Extraction Wizard which is a freeware tool.
5# Extract Images From Excel Using Imageoptions Class And Getimages Methods
Another method that you can use to extract images from Excel spreadsheets is by using the Imageoptions Class And Getimages Methods.
Using this method images are been extracted in their original format.
This ImageOptions class helps in the easy extraction of images from Excel workbook in the following format gif, WEBP, jpeg, BMP, png.
Whereas, the getImages method will return you the null value if in case of image extraction won’t support for Excel file.
If image extraction won’t be supported by the CSV file then you will get the null value from “getImages” method.
Here I have listed down the steps for extracting images in png format from Excel spreadsheet:
Here is the complete example to show you how to extract images from Excel workbook.
Copy// Create an instance of Parser class
try (Parser parser = new Parser(Constants.SampleWithImagesXlsx)) {
// Extract images from spreadsheet
Iterable<PageImageArea> images = parser.getImages();
// Create the options to save images in PNG format
ImageOptions options = new ImageOptions(ImageFormat.Png);
int imageNumber = 0;
// Iterate over images
for (PageImageArea image : images)
{
// Save the image to the png file
image.save(Constants.getOutputFilePath(String.format(“%d.png”, imageNumber)), options);
imageNumber++;
}
}
Wrapping It Up:
Extracting pictures from Excel file is not that tough if you know the right way to do it. I hope this blog will help you to accomplish this task in an effortless manner.
Besides this, if you end up losing your Excel file data either due to corruption or damage issue. In that case, you can go with the recommended solution Excel repair tool. As it is the best tool to repair corrupt Excel file data.
This tool very efficiently repair corrupt Excel xlsx/xls files and recover objects, formulas, including shared formula and array-entered formula.
If you find this tutorial helpful then do share your experiences with us….!