|
@@ -9,10 +9,12 @@ import json
|
|
import os
|
|
import os
|
|
import time
|
|
import time
|
|
from urllib.parse import unquote
|
|
from urllib.parse import unquote
|
|
|
|
+from docx import Document
|
|
|
|
+from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
|
|
+
|
|
|
|
|
|
import lark_oapi as lark
|
|
import lark_oapi as lark
|
|
import tos
|
|
import tos
|
|
-from docx import Document
|
|
|
|
from lark_oapi.api.drive.v1 import CreateExportTaskRequest, ExportTask, CreateExportTaskResponse, GetExportTaskRequest, \
|
|
from lark_oapi.api.drive.v1 import CreateExportTaskRequest, ExportTask, CreateExportTaskResponse, GetExportTaskRequest, \
|
|
GetExportTaskResponse, DownloadExportTaskRequest, DownloadExportTaskResponse
|
|
GetExportTaskResponse, DownloadExportTaskRequest, DownloadExportTaskResponse
|
|
from tos import HttpMethodType
|
|
from tos import HttpMethodType
|
|
@@ -51,7 +53,48 @@ def f_upload_file(save_path) -> str:
|
|
print('fail with unknown error: {}'.format(e))
|
|
print('fail with unknown error: {}'.format(e))
|
|
|
|
|
|
|
|
|
|
-def f_doc_export(token: str, data: object) -> str:
|
|
+
|
|
|
|
+def create_word_table(json_data):
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ json_data = json.loads(json_data)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ document = Document()
|
|
|
|
+
|
|
|
|
+ table = document.add_table(rows=len(json_data['data']), cols=len(json_data['data'][0]))
|
|
|
|
+
|
|
|
|
+ for i, row in enumerate(json_data['data']):
|
|
|
|
+ for j, cell_value in enumerate(row):
|
|
|
|
+ cell = table.cell(i, j)
|
|
|
|
+ cell.text = cell_value.strip()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ table.style = 'Table Grid'
|
|
|
|
+ for row in table.rows:
|
|
|
|
+ for cell in row.cells:
|
|
|
|
+ cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for merge in json_data.get('merges', []):
|
|
|
|
+ start_cell = table.cell(merge['start_row'], merge['start_column'])
|
|
|
|
+ end_cell = table.cell(merge['end_row'], merge['end_column'])
|
|
|
|
+ start_cell.merge(end_cell)
|
|
|
|
+
|
|
|
|
+ all_text = ""
|
|
|
|
+ for paragraph in start_cell.paragraphs:
|
|
|
|
+ all_text += paragraph.text
|
|
|
|
+
|
|
|
|
+ for paragraph in start_cell.paragraphs:
|
|
|
|
+ p = paragraph._element
|
|
|
|
+ p.getparent().remove(p)
|
|
|
|
+ p._p = p._element = None
|
|
|
|
+
|
|
|
|
+ start_cell.add_paragraph(all_text)
|
|
|
|
+
|
|
|
|
+ return table
|
|
|
|
+
|
|
|
|
+def f_doc_export(token: str, request_id: str, data: object) -> str:
|
|
|
|
|
|
app_id = BaseConfig.app_id
|
|
app_id = BaseConfig.app_id
|
|
app_secret = BaseConfig.app_secret
|
|
app_secret = BaseConfig.app_secret
|
|
@@ -129,17 +172,16 @@ def f_doc_export(token: str, data: object) -> str:
|
|
|
|
|
|
if data is not None:
|
|
if data is not None:
|
|
doc = Document(save_path)
|
|
doc = Document(save_path)
|
|
- placeholder = ""
|
|
+ placeholder = "{TABLE_PLACEHOLDER}"
|
|
for paragraph in doc.paragraphs:
|
|
for paragraph in doc.paragraphs:
|
|
if not placeholder in paragraph.text:
|
|
if not placeholder in paragraph.text:
|
|
continue
|
|
continue
|
|
|
|
|
|
for run in paragraph.runs:
|
|
for run in paragraph.runs:
|
|
run.text = run.text.replace(placeholder, "")
|
|
run.text = run.text.replace(placeholder, "")
|
|
-
|
|
+
|
|
- table = doc.add_table(rows=1, cols=[])
|
|
+ table = create_word_table(data)
|
|
-
|
|
+ paragraph._element.addnext(table._tbl)
|
|
- paragraph._element.addnext(table._element)
|
|
|
|
doc.save(save_path)
|
|
doc.save(save_path)
|
|
time.sleep(2)
|
|
time.sleep(2)
|
|
|
|
|