1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from spire.xls import Workbook from spire.xls.common import * # Import necessary modules from Pillow from PIL import Image # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile("file.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Save the worksheet to an image stream image_stream = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn) # Save the stream to a file (Spire may give you a stream, which you need to convert first) with open("SheetToImage.png", "wb") as f: f.write(image_stream.ToArray()) # Open and manipulate the image with Pillow, if needed image = Image.open("SheetToImage.png") image.show() # Opens the image (optional) image.save("SheetToImage_converted.png") # Save as PNG or any other format # Dispose of the workbook resources workbook.Dispose() |