Created
September 21, 2022 10:00
-
-
Save smallyunet/450084daccad56fd0abae4169b1e9922 to your computer and use it in GitHub Desktop.
handle excel and img
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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