Browse Source

fix: 自动拆解

master
kbb 1 month ago
parent
commit
6fb2a6aa60
2 changed files with 33 additions and 11 deletions
  1. +27
    -8
      docx_extract.py
  2. +6
    -3
      flask_server.py

+ 27
- 8
docx_extract.py View File

@@ -1,17 +1,18 @@
import os
import docx
import requests
import mysql_pool
from pymysql.converters import escape_string
import xlsxwriter
import uuid


def read_docx(file_path):
mysql = mysql_pool.ConnMysql()
# print(os.path.abspath('丽水市本级信息化项目建设方案模板.docx'))
# # 通过url获取文件 http://jobapi.ningdatech.com/prometheus-yw_file_service/files/20240116/5a75cb43d17d4f1589d455d21547ab0c.doc
# url = "http://jobapi.ningdatech.com/prometheus-yw_file_service/files/20240919/669f323c5c824f89a34bf04a66105902.doc"
# file_name = "丽水市本级信息化项目建设方案模板.docx"
# file_path = os.path.join("temp", file_name)
excel_path = f"/Users/kebobo/fsdownload/dup_check/temp/{uuid.uuid4()}.xls"
try:
# r = requests.get(url)
# with open(file_path, "wb") as code:
@@ -63,18 +64,36 @@ def read_docx(file_path):
feature_name = para.text

# 使用next函数逐个获取元素
# for key, value in feature_map.items():
# if key != "3.1.2 建设内容" and key != "":
# print(f"Key: {key}, Value: {value}")
# # 将功能描述入库
# mysql.sql_change_msg(
# """insert into user_history_module_data(xmmc,gnmc,gnms,line, remark) value("%s", "%s", "%s", "%s", "%s")""" % (
# escape_string(xmmc), escape_string(key), escape_string(value), "", "自动拆解导入"))

# 生成excel文件(兼容老版本, 减少改动量)
workbook = xlsxwriter.Workbook(excel_path)
worksheet = workbook.add_worksheet("Sheet1")
worksheet.write('A1', '项目名称')
worksheet.write('B1', escape_string(xmmc))

i = 0
for key, value in feature_map.items():
if key != "3.1.2 建设内容" and key != "":
print(f"Key: {key}, Value: {value}")
# 将功能描述入库
mysql.sql_change_msg(
"""insert into user_history_module_data(xmmc,gnmc,gnms,line, remark) value("%s", "%s", "%s", "%s", "%s")""" % (
escape_string(xmmc), escape_string(key), escape_string(value), "", "自动拆解导入"))
i = i + 1
worksheet.write(f"A{i + 1}", '功能模块')
worksheet.write(f"B{i + 1}", escape_string(key))
worksheet.write(f"C{i + 1}", '功能描述')
worksheet.write(f"D{i + 1}", escape_string(value))
workbook.close()
return excel_path


finally:
# os.remove(file_path)
print("删除文件")
# print("删除文件")
print("转换完成")

return "\n".join(content)



+ 6
- 3
flask_server.py View File

@@ -17,7 +17,7 @@ app = Flask(__name__)
# 返回excel的保存地址
@app.route('/check/duplicates/<projectId>')
def success(projectId):
# file_type = request.args.get('fileType', 'excel')
file_type = request.args.get('fileType', 'excel')
mysql=mysql_pool.ConnMysql()
if int(projectId) == 0:
data = mysql.sql_select_many("""select * from idc_project""")
@@ -28,11 +28,14 @@ def success(projectId):

for ap in data:
# if os.path.exists(ap.get("file_path")):
# 如果文件类型为docx 则转换为excel
if file_type != "excel":
new_path = docx_extract.read_docx(ap.get("file_path"))
ap["file_path"] = new_path

data_list.append((ap.get("project_id"), ap.get("file_path"), ap.get("project_name")))

# data_list.append((ap.get("project_id"), "C:/Users/PoffyZhang/Desktop/花园云(城市大脑)数字驾驶舱20230202.xls", ap.get("project_name")))
mysql.release()
# print(data_list)
main1.project_check(data_list)
return jsonify({"code": 0, "data": data})



Loading…
Cancel
Save