Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions office/lib/excel/SplitExcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import xlrd, xlwt
import openpyxl
import datetime
from tqdm import tqdm


#
Expand Down Expand Up @@ -41,8 +42,8 @@ def process_xls(filepath: str, column: int, worksheet_name: str = None) -> str:
"""
try:
workbook = xlrd.open_workbook(filepath, formatting_info=True)
except:
return "文件读取异常:{}".format(filepath)
except Exception as e:
return "文件读取异常:{},错误信息:{}".format(filepath, str(e))
if worksheet_name:
worksheet = workbook.sheet_by_name(worksheet_name)
else:
Expand Down Expand Up @@ -94,22 +95,26 @@ def process_xlsx(filepath: str, column: int, worksheet_name: str = None) -> str:
"""
try:
workbook = openpyxl.load_workbook(filepath, read_only=True, data_only=True)
except:
return "文件读取异常:{}".format(filepath)
if worksheet_name:
worksheet = workbook.get_sheet_by_name(worksheet_name)
else:
worksheet = workbook.active
if worksheet.max_column < column:
return "最大列数是{},取不到第{}列".format(worksheet.max_column, column)
try:
if worksheet_name:
worksheet = workbook[worksheet_name]
else:
worksheet = workbook.active
if worksheet.max_column < column:
return "最大列数是{},取不到第{}列".format(worksheet.max_column, column)

split_data_dict = {}
for row in worksheet.rows:
row_data = [cell.value if cell.value else ' ' for cell in row]
temp_data = row_data[column - 1]
temp_data_list = split_data_dict.get(temp_data, [])
temp_data_list.append(row_data)
split_data_dict[temp_data] = temp_data_list
split_data_dict = {}
for row in worksheet.rows:
row_data = [cell.value if cell.value else ' ' for cell in row]
temp_data = row_data[column - 1]
temp_data_list = split_data_dict.get(temp_data, [])
temp_data_list.append(row_data)
split_data_dict[temp_data] = temp_data_list
finally:
workbook.close()
except Exception as e:
return "处理异常:{},错误信息:{}".format(filepath, str(e))

new_filepath = generate_xlsx(filepath, split_data_dict)
return "数据保存在新文件中,文件名:{}".format(new_filepath)

Expand Down
11 changes: 4 additions & 7 deletions office/lib/image/eliminate_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def _hex_to_rgb(hex):
十六进制转RGB
"""
if hex[0] != '#' or len(hex) != 7:
print('注意:十六进制格式颜色错误,请输入7位以\'#\'开头的字符串\n')
return None
raise ValueError('十六进制格式颜色错误,请输入7位以\'#\'开头的字符串,例如 #FFFFFF')
else:
r = int('0x' + hex[1:3], 16)
g = int('0x' + hex[3:5], 16)
Expand Down Expand Up @@ -38,11 +37,9 @@ def eliminate_bc(src_img_path: str, save_img_path: str, margin: int = 30, bc_col
r, g, b = bc_color
else:
# 未给定背景色,拾取图片左上角颜色作为背景色
pix = img.load()
if src_img_path.endswith('.jpg'):
r, g, b = pix[int(width / 20), int(height / 20)]
elif src_img_path.endswith('.png'):
r, g, b, _ = pix[int(width / 20), int(height / 20)]
rgb_img = img.convert("RGB")
pix = rgb_img.load()
r, g, b = pix[int(width / 20), int(height / 20)]

img = img.convert("RGBA")
datas = img.getdata()
Expand Down