mdbq 1.8.1__py3-none-any.whl → 1.8.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mdbq/other/sku_picture.py CHANGED
@@ -22,11 +22,10 @@ from mdbq.mysql import mysql
22
22
  from mdbq.mysql import s_query
23
23
  import ua_sj
24
24
  import requests
25
+
26
+ import io
25
27
  from openpyxl import load_workbook
26
- # from openpyxl.drawing.image import Image
27
- import openpyxl
28
- from PIL import Image as PILImage
29
- from io import BytesIO
28
+ from openpyxl.drawing.image import Image
30
29
  from openpyxl.utils import get_column_letter
31
30
 
32
31
  warnings.filterwarnings('ignore')
@@ -571,77 +570,146 @@ def main2(service_name, database):
571
570
 
572
571
  class InsertPicture():
573
572
  def __init__(self):
574
- self.file = '/Users/xigua/Downloads/test.xlsx'
575
- self.path = '/Users/xigua/Downloads/sku图片链接'
573
+ self.filename = 'test.xlsx'
574
+ self.path = '/Users/xigua/Downloads'
576
575
  self.pic_datas = []
577
576
  self.header = 0 # sku 的标题栏起始行数
578
577
 
579
578
  def insert_data(self):
580
579
  self.get_filename()
581
- # sku_in_files = [item['sku'] for item in self.pic_datas]
582
- # print(len(sku_in_files))
583
-
584
- # df = pd.read_excel(self.file, header=self.header)
585
- # cols = df.columns.tolist()
586
- # print(cols)
587
580
 
588
- workbook = load_workbook(self.file)
581
+ workbook = load_workbook(os.path.join(self.path, self.filename))
589
582
  sheet = workbook.active
590
583
  rows = sheet.max_row # 总行数
591
584
  columns = sheet.max_column # 总列数
592
- # print(columns)
593
- # print(rows)
594
585
  sheet.insert_cols(0, 1) # 在第0列开始插入1列空白列
595
- sheet['A1'] = '商品图片'
586
+ # sheet['A1'] = '商品图片'
596
587
 
597
- for col in range(1, columns+1):
598
- for row in range(1, rows+1):
588
+ is_trange = False
589
+ for col in range(1, columns+1): # 遍历每一列
590
+ # if is_trange == True:
591
+ # break
592
+ for row in range(1, rows+1): # 遍历每一行
599
593
  # print(f'第{col}列, 第{row}行...')
600
594
  value = sheet.cell(row=row, column=col).value
601
595
  if value:
602
596
  for data in self.pic_datas:
603
- if str(value) == data['sku']:
604
- print(value, data['文件名称'])
597
+ if str(value) == data['sku'] or (len(str(value)) > 16 and str(value) in data['sku']):
598
+ # print(value, data['sku'])
599
+ print(f'转换: 第{col}列, 第{row}行, sku: {data['sku']} ...')
605
600
  image_path = os.path.join(data['文件路径'], data['文件名称'])
606
- img = PILImage.open(image_path)
607
- output = BytesIO()
608
- img.save(output, format='JPEG')
609
- image_data = output.getvalue()
610
-
611
- # 调整图片大小
612
- img_resized = img.resize((128, 128)) # 调整为128x128大小
613
- output_resized = BytesIO()
614
- img_resized.save(output_resized, format='JPEG')
615
- image_data_resized = output_resized.getvalue()
601
+ with open(image_path, 'rb') as f:
602
+ img_data = f.read()
603
+ img = Image(io.BytesIO(img_data))
604
+ width, height = self.img_resize(img.width, img.height) # 等比例缩放图片
616
605
  col_letter = 'A'
617
- sheet.add_image(openpyxl.drawing.image.Image(sheet, image_data_resized), f'{col_letter}{row}')
618
- break
619
-
620
- # workbook.save(self.file)
621
- # print(filenames)
622
- # sheet.insert_cols(0, 1) # 在第0列开始插入1列空白列
623
- # sheet['A1'] = '商品图片'
624
-
625
-
626
- # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
627
- # sheet.column_dimensions[col_letter].width = 10
628
- # sheet.row_dimensions[row].height = 80
629
- # # sheet.cell(row=row, column=col).value = "" # 删除原内容
630
- # now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ")
631
- # print(f'{now}正在转换: 第{col}列, 第{row}行..')
606
+ # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
607
+ sheet.column_dimensions[col_letter].width = 13
608
+ sheet.row_dimensions[row].height = 80
609
+ img.width = width
610
+ img.height = height
611
+ sheet.add_image(img, f'{col_letter}{row}')
612
+ is_trange = True
613
+
614
+ if is_trange == False: # 如果 sku 没有匹配到任何值,则使用 商家编码
615
+ for col in range(1, columns + 1): # 遍历每一列
616
+ # if is_trange == True:
617
+ # break
618
+ for row in range(1, rows + 1): # 遍历每一行
619
+ # print(f'第{col}列, 第{row}行...')
620
+ value = sheet.cell(row=row, column=col).value
621
+ if value:
622
+ for data in self.pic_datas:
623
+ if str(value) == data['商家编码']:
624
+ # print(value, data['sku'])
625
+ print(f'转换: 第{col}列, 第{row}行, 商家编码: {data['商家编码']} ...')
626
+ image_path = os.path.join(data['文件路径'], data['文件名称'])
627
+ with open(image_path, 'rb') as f:
628
+ img_data = f.read()
629
+ img = Image(io.BytesIO(img_data))
630
+ width, height = self.img_resize(img.width, img.height) # 等比例缩放图片
631
+ col_letter = 'A'
632
+ # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
633
+ sheet.column_dimensions[col_letter].width = 13
634
+ sheet.row_dimensions[row].height = 80
635
+ img.width = width
636
+ img.height = height
637
+ sheet.add_image(img, f'{col_letter}{row}')
638
+ is_trange = True
639
+ break # 商家编码只需要添加一次,所以必须 break,否则可能添加多个图片到某个单元格
640
+
641
+ if is_trange == False: # 如果 sku 和商家编码都没有匹配到任何值,则使用 商品id
642
+ for col in range(1, columns + 1): # 遍历每一列
643
+ # if is_trange == True:
644
+ # break
645
+ for row in range(1, rows + 1): # 遍历每一行
646
+ # print(f'第{col}列, 第{row}行...')
647
+ value = sheet.cell(row=row, column=col).value
648
+ if value:
649
+ for data in self.pic_datas:
650
+ if str(value) == data['商品id']:
651
+ # print(value, data['sku'])
652
+ print(f'转换: 第{col}列, 第{row}行, 商品id: {data['商品id']} ...')
653
+ image_path = os.path.join(data['文件路径'], data['文件名称'])
654
+ with open(image_path, 'rb') as f:
655
+ img_data = f.read()
656
+ img = Image(io.BytesIO(img_data))
657
+ width, height = self.img_resize(img.width, img.height) # 等比例缩放图片
658
+ col_letter = 'A'
659
+ # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
660
+ sheet.column_dimensions[col_letter].width = 13
661
+ sheet.row_dimensions[row].height = 80
662
+ img.width = width
663
+ img.height = height
664
+ sheet.add_image(img, f'{col_letter}{row}')
665
+ is_trange = True
666
+ break # 商品id只需要添加一次,所以必须 break,否则可能添加多个图片到某个单元格
667
+
668
+ if is_trange == False:
669
+ print(f'{self.filename}:\n'
670
+ f'在该文件中没有找到匹配的 skuid/商品id/商家编码, 注意程序只会转换当前活动的 sheet, \n'
671
+ f'1. 如果您确定文件中确实存在 skuid/商品id/商家编码, 可能是因为 sheet 不是活动状态, 请切换后再重新运行本程序。\n'
672
+ f'2. 程序只能转换已经收录的商品图, 如果未被收录亦会转换失败, 请联系开发者添加。')
673
+
674
+ workbook.save(os.path.join(self.path, f'ok_{self.filename}'))
675
+
676
+ def img_resize(self, width, height, num=100):
677
+ """
678
+ 设置基础大小为 num, 并等比例缩放
679
+ """
680
+ if width > height:
681
+ height = height * num // width
682
+ width = num
683
+ else:
684
+ width = width * num // height
685
+ height = num
686
+ return width, height
632
687
 
633
688
  def get_filename(self):
634
- for root, dirs, files in os.walk(self.path, topdown=False):
689
+ for root, dirs, files in os.walk(os.path.join(self.path, 'sku图片链接'), topdown=False):
635
690
  for name in files:
636
691
  if name.endswith('.jpg'):
637
692
  sku_id = re.findall(r'\d+_\d+_(\d+)_|\d+_\d+_(\d+-\d+)_|\d+_\d+_([A-Za-z]+\d+)_', name)
638
693
  sku_id = [item for item in sku_id[0] if item != '']
639
- self.pic_datas.append({'文件路径': root, '文件名称': name, 'sku': sku_id[0]})
694
+ sp_id = re.findall(r'^(\d+)_', name)
695
+ spbm = re.findall(r'(\d{13})\d+', sku_id[0])
696
+ if not spbm:
697
+ spbm = ['0']
698
+ self.pic_datas.append(
699
+ {
700
+ '文件路径': root,
701
+ '文件名称': name,
702
+ 'sku': sku_id[0],
703
+ '商品id': sp_id[0],
704
+ '商家编码': spbm[0],
705
+ }
706
+ )
640
707
 
641
708
 
642
709
  def main3():
643
710
  p = InsertPicture()
644
- p.header = 1
711
+ p.filename = 'test222.xlsx'
712
+ # p.header = 1
645
713
  p.insert_data()
646
714
 
647
715
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 1.8.1
3
+ Version: 1.8.2
4
4
  Home-page: https://pypi.org/project/mdbsql
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -30,13 +30,13 @@ mdbq/mysql/year_month_day.py,sha256=VgewoE2pJxK7ErjfviL_SMTN77ki8GVbTUcao3vFUCE,
30
30
  mdbq/other/__init__.py,sha256=jso1oHcy6cJEfa7udS_9uO5X6kZLoPBF8l3wCYmr5dM,18
31
31
  mdbq/other/porxy.py,sha256=UHfgEyXugogvXgsG68a7QouUCKaohTKKkI4RN-kYSdQ,4961
32
32
  mdbq/other/pov_city.py,sha256=AEOmCOzOwyjHi9LLZWPKi6DUuSC-_M163664I52u9qw,21050
33
- mdbq/other/sku_picture.py,sha256=sf-3i4sfk8kQUZgXMXlg6aiibw2BJF38tVr-ULUi8TI,28937
33
+ mdbq/other/sku_picture.py,sha256=xNXOzyaO-qz1htYroPmYnsct_CXF4MLJASereVEGN6g,33429
34
34
  mdbq/other/ua_sj.py,sha256=JuVYzc_5QZ9s_oQSrTHVKkQv4S_7-CWx4oIKOARn_9U,22178
35
35
  mdbq/pbix/__init__.py,sha256=Trtfaynu9RjoTyLLYBN2xdRxTvm_zhCniUkVTAYwcjo,24
36
36
  mdbq/pbix/pbix_refresh.py,sha256=JUjKW3bNEyoMVfVfo77UhguvS5AWkixvVhDbw4_MHco,2396
37
37
  mdbq/pbix/refresh_all.py,sha256=0uAnBKCd5cx5FLTkawN1GV9yi87rfyMgYal5LABtumQ,7186
38
38
  mdbq/spider/__init__.py,sha256=RBMFXGy_jd1HXZhngB2T2XTvJqki8P_Fr-pBcwijnew,18
39
- mdbq-1.8.1.dist-info/METADATA,sha256=4KLPOuQB3iLxcdwbEQ39prUCY6TNJJTnLZTwEbNNlYA,245
40
- mdbq-1.8.1.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
41
- mdbq-1.8.1.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
42
- mdbq-1.8.1.dist-info/RECORD,,
39
+ mdbq-1.8.2.dist-info/METADATA,sha256=PsbFqqZUVz03tpO86vKRA68rj6Oy3JtEQ2qOUqnauQI,245
40
+ mdbq-1.8.2.dist-info/WHEEL,sha256=cpQTJ5IWu9CdaPViMhC9YzF8gZuS5-vlfoFihTBC86A,91
41
+ mdbq-1.8.2.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
42
+ mdbq-1.8.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.44.0)
2
+ Generator: setuptools (70.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5