Python Forum
New2Python: Help with Importing/Mapping Image Src to Image Code in File - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: New2Python: Help with Importing/Mapping Image Src to Image Code in File (/thread-38729.html)



New2Python: Help with Importing/Mapping Image Src to Image Code in File - CluelessITguy - Nov-17-2022

I am creating an item catalogue of our products and need to import the item images besides their product codes'. The Image Code in the file is the same as the image saved name.

I've automated a SQL query for the table and connected it to Python, exporting it into an Excel file.

## SQL Query
sql_query = pd.read_sql_query('''

SELECT TOP 250 '' AS 'Image', LEFT(stockcode, 4)+RIGHT(stockcode, 4) AS 'Image Code', [stockcode], [Description]
FROM V_Sale_TurnOver sto
WHERE [PostingDate] BETWEEN '2022-01-01' AND getdate()
GROUP BY [Range], [stockcode], [Description]

    ''' ,conn) 

df = pd.DataFrame(sql_query)

## Export to Folder
df.to_csv (r'G:\Outbound\Reports\Top 250 SKU/Top250SKU.csv', index = False)
I need to map the product images to their image codes' (i.e., if a product Image Code = 'AURO0002', I need to import and map it to the saved image file 'AURO0002').

Also, if the Image Code does not map to anything, I need the code to prevent errors from occurring. If it helps, below is a similar script written on VBA Script for a similar task.

# VBA Code
rowNo = 1

dim ImgPath As String, NoImagePath As String
NoImagePath = "\\g_drive\Images\NoImage.jpg"
While Top250SKU.cells(rowNo, 2).Value <>
    If Rs2.RecordCount <> 0 Then Rs2.MoveFirst
    Do While Not Rs2.EOF

        If ActiveSheet.Cells(rowNo, 2).Value = Rs2!stockcode And _
                ActiveSheet.Cells(rowNo, 3).Value = Rs2!Description Then

       End If
        Rs2.MoveNext
    Loop
    ActiveSheet.Cells(4, 1).Value = "Image"

   ImgPath = "\\g_drive\Images\" & ActiveSheet.Cells(rowNo, 2).Value & ".jpg"
    If FileExists(ImgPath) = False Then
        ImgPath = NoImagePath
    End If
    Rows(rowNo & ":" & rowNo).RowHeight = 60
    Call InsertPictureIG(ImgPath, ActiveSheet.Cells(rowNo, 1).Address)
rowNo = rowNo + 1
How can I go about coding this?