Spaces:
Running
Running
change excel
Browse files- excel/excel_translate.py +18 -21
excel/excel_translate.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import
|
2 |
from typing import Dict, List
|
3 |
from translate.translator import translate_text_dict
|
4 |
import math
|
@@ -25,24 +25,23 @@ def translate_xlsx(file_id: str, sheet_name: str = None, from_lang: str = 'en',
|
|
25 |
temp_file.write(file_data)
|
26 |
temp_file_path = temp_file.name
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
wb = xw.Book(temp_file_path) # Mở workbook từ file tạm
|
31 |
|
32 |
# Chọn sheet được chỉ định hoặc tất cả các sheet
|
33 |
-
sheets = [wb
|
34 |
|
35 |
-
for
|
36 |
-
|
37 |
-
|
38 |
|
39 |
# Tạo dictionary lưu trữ nội dung cần dịch và mapping từ key đến cell
|
40 |
text_dict: Dict[str, List[str]] = {}
|
41 |
cell_map: Dict[str, any] = {} # lưu mapping key -> cell object
|
42 |
|
43 |
-
for row in range(1,
|
44 |
-
for col in range(1,
|
45 |
-
cell =
|
46 |
if isinstance(cell.value, str):
|
47 |
key = f"R{row}C{col}" # key theo dạng R{row}C{col}
|
48 |
text_dict[key] = [cell.value] # Lưu giá trị dưới dạng danh sách với 1 phần tử
|
@@ -59,18 +58,16 @@ def translate_xlsx(file_id: str, sheet_name: str = None, from_lang: str = 'en',
|
|
59 |
cell.value = translated_text_list[0]
|
60 |
|
61 |
# Lưu workbook vào file tạm thời
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
wb.close()
|
64 |
-
app.quit()
|
65 |
-
|
66 |
-
# Đọc lại file tạm để lưu vào MongoDB
|
67 |
-
with open(temp_file_path, "rb") as f:
|
68 |
-
translated_file_id = fs_output.put(f, filename=f"translated_{file_id}.xlsx")
|
69 |
-
|
70 |
-
# Xóa file tạm
|
71 |
os.remove(temp_file_path)
|
72 |
-
|
73 |
-
print(f"
|
74 |
return translated_file_id
|
75 |
|
76 |
|
|
|
1 |
+
import openpyxl
|
2 |
from typing import Dict, List
|
3 |
from translate.translator import translate_text_dict
|
4 |
import math
|
|
|
25 |
temp_file.write(file_data)
|
26 |
temp_file_path = temp_file.name
|
27 |
|
28 |
+
# Đọc file Excel bằng openpyxl
|
29 |
+
wb = openpyxl.load_workbook(temp_file_path)
|
|
|
30 |
|
31 |
# Chọn sheet được chỉ định hoặc tất cả các sheet
|
32 |
+
sheets = [wb[sheet_name]] if sheet_name else wb.worksheets
|
33 |
|
34 |
+
for ws in sheets:
|
35 |
+
max_row = ws.max_row
|
36 |
+
max_col = ws.max_column
|
37 |
|
38 |
# Tạo dictionary lưu trữ nội dung cần dịch và mapping từ key đến cell
|
39 |
text_dict: Dict[str, List[str]] = {}
|
40 |
cell_map: Dict[str, any] = {} # lưu mapping key -> cell object
|
41 |
|
42 |
+
for row in range(1, max_row + 1):
|
43 |
+
for col in range(1, max_col + 1):
|
44 |
+
cell = ws.cell(row=row, column=col)
|
45 |
if isinstance(cell.value, str):
|
46 |
key = f"R{row}C{col}" # key theo dạng R{row}C{col}
|
47 |
text_dict[key] = [cell.value] # Lưu giá trị dưới dạng danh sách với 1 phần tử
|
|
|
58 |
cell.value = translated_text_list[0]
|
59 |
|
60 |
# Lưu workbook vào file tạm thời
|
61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as output_file:
|
62 |
+
wb.save(output_file.name)
|
63 |
+
output_file.seek(0)
|
64 |
+
translated_file_id = fs_output.put(output_file.read(), filename=f"translated_{file_id}.xlsx")
|
65 |
+
|
66 |
+
# Đóng workbook và xóa file tạm
|
67 |
wb.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
os.remove(temp_file_path)
|
69 |
+
|
70 |
+
print(f"✅ Dịch thành công! File đã lưu vào MongoDB với file_id: {translated_file_id}")
|
71 |
return translated_file_id
|
72 |
|
73 |
|