Skip to content

Instantly share code, notes, and snippets.

@smallyunet
Created September 21, 2022 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smallyunet/450084daccad56fd0abae4169b1e9922 to your computer and use it in GitHub Desktop.
Save smallyunet/450084daccad56fd0abae4169b1e9922 to your computer and use it in GitHub Desktop.
handle excel and img
#Importing the modules
import openpyxl
from openpyxl_image_loader import SheetImageLoader
#loading the Excel File and the sheet
pxl_doc = openpyxl.load_workbook('imgs2.xlsx')
# print(pxl_doc.sheetnames)
sheet = pxl_doc[pxl_doc.sheetnames[0]]
# # #calling the image_loader
image_loader = SheetImageLoader(sheet)
a = 0
for i in range(2, 1362):
if not image_loader.image_in("K" + str(i)):
print(str(i))
a += 1
continue
#get the image (put the cell you need instead of 'A1')
image = image_loader.get('K' + str(i))
image = image.convert('RGB')
# #showing the image
# image.show()
# filename = 'img/' + f"{i:05d}" + '.jpg'
filename = sheet['L' + str(i)].value
# #saving the image
image.save("img/" + filename)
print(a)
import xlsxwriter
workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()
for i in range(0, 979):
name = f"{i+1:05d}" + '.jpg'
worksheet.write(i, 0, name) # Writes an int
workbook.close()
import xlsxwriter
from os.path import exists
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('picture.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('K:K', 15)
image_width = 70.0
image_height = 106.0
cell_width = 80.0
cell_height = 120.0 -10
x_scale = cell_width/image_width
y_scale = cell_height/image_height
for i in range(0, 999):
filename = 'tmp3/' + f"{i:05d}" + '.jpg'
file_exists = exists(filename)
if not file_exists:
continue
data = f"{i:05d}" + '.jpg'
name = "K" + str(i)
nameB = "L" + str(i)
worksheet.set_default_row(80)
worksheet.write(nameB, data)
worksheet.insert_image(name, filename, {'x_scale': x_scale/4, 'y_scale': y_scale/4})
workbook.close()
import os
from pypinyin import pinyin
from pypinyin import lazy_pinyin
from os.path import exists
names = [
]
ids = [
]
if names.__len__() != ids.__len__():
print("names and ids are not equal")
exit(1)
entries = os.listdir('tmp/')
for entry in entries:
for i in range(0, ids.__len__()):
if entry.startswith(ids[i]):
old = 'tmp/' + entry
new = 'tmp2/' + f"{i+1:05d}" + '.jpg'
file_exists = exists(old)
if not file_exists:
continue
os.rename(old, new)
print(old, new)
from PIL import Image
from os.path import exists
for i in range(0, 979):
old = 'tmp2/' + f"{i+1:05d}" + '.jpg'
new = 'tmp3/' + f"{i+1:05d}" + '.jpg'
file_exists = exists(old)
if not file_exists:
continue
image = Image.open(old)
image = image.convert('RGB')
new_image = image.resize((295, 413))
new_image.save(new)
import xlsxwriter
from os.path import exists
for i in range(1, 1359):
filename = 'img/' + f"{i:05d}" + '.jpg'
file_exists = exists(filename)
if not file_exists:
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment