Example #1
0
def merge_excels():
    pythoncom.CoInitialize()
    xlApp = DispatchEx('Excel.Application')
    xlApp.Visible = False
    xlApp.DisplayAlerts = 0
    xlApp.ScreenUpdating = 0
    file = xlwt.Workbook()
    ta = file.add_sheet('sheet1')
    index = 0
    try:
        excels = os.listdir(".//files")
    except:
        print 'can not find directory files'
        return
    for xfile in excels:
        indexMark = index
        if xfile[0] == '~':
            continue
        postfix = xfile.split(".")[-1]
        if postfix == "xls" or postfix == "xlsx":
            print "Merging " + xfile
            absPath = os.path.abspath(r"files\\" + xfile)
            index = xlrd_merge(index, absPath, ta)
            if (index == indexMark):
                index = win32com_merge(index, absPath, xlApp, ta)
            index = index - 1
    file.save("merged.xls")
    xlApp.Quit()
    pythoncom.CoUninitialize()
Example #2
0
def upload():
    """Handle the upload of a file."""
    form = request.form

    # Create a unique "session ID" for this particular batch of uploads.
    upload_key = str(uuid4())

    # Is the upload using Ajax, or a direct POST by the form?
    is_ajax = False
    if form.get("__ajax", None) == "true":
        is_ajax = True

    # Target folder for these uploads.
    target = "uploadr/static/uploads/{}".format(upload_key)
    try:
        os.mkdir(target)
        os.mkdir(target + "/input")
        os.mkdir(target + "/output")
    except:
        if is_ajax:
            return ajax_response(
                False, "Couldn't create upload directory: {}".format(target))
        else:
            return "Couldn't create upload directory: {}".format(target)

    print("=== Form Data ===")
    for key, value in list(form.items()):
        print(key, "=>", value)

    destination = ""
    for upload in request.files.getlist("file"):
        filename = upload.filename.rsplit("/")[0]
        destination = "/".join([target, filename])
        print("Accept incoming file:", filename)
        print("Save it to:", destination)
        upload.save(destination)

    # gaoxz
    print(destination)
    zf = zipfile.ZipFile(destination, "r")
    for fileM in zf.namelist():
        suffix = os.path.splitext(fileM)[-1]
        if suffix == ".xlsx":
            zf.extract(fileM, target + "/input/")
        else:
            zf.extract(fileM, target + "/input")
    zf.close()

    shutil.copy("c:\\FormMaker.xlsm", target + "/input/FormMaker.xlsm")

    # gaoxz2
    pythoncom.CoInitialize()
    path = target + "/input"  # 文件夹目录
    files = os.listdir(path)  # 得到文件夹下的所有文件名称
    for file in files:  # 遍历文件夹
        if not os.path.isdir(file):  # 判断是否是文件夹,不是文件夹才打开
            suffix = os.path.splitext(file)[-1]
            if suffix == ".xlsx":
                excel = DispatchEx('excel.application')
                xlsx_fullname = os.path.abspath(target + "/input/" + file)
                excel.Visible = True
                excel.DisplayAlerts = False  # 关闭系统警告
                excel.ScreenUpdating = False  # 关闭屏幕刷新

                # 打开Excel文件
                w1 = excel.workbooks.Open(xlsx_fullname)
                # w2 = excel.workbooks.Open('D:\\Normandy\\OperationOverlord\\\OperationPointblank\\FormMaker.xlsm', ReadOnly=1)
                xlsx_fullname = os.path.abspath(target +
                                                "/input/FormMaker.xlsm")
                w2 = excel.workbooks.Open(xlsx_fullname, ReadOnly=1)

                # 其他操作代码
                # ...
                print(w1.Worksheets(1).Range("G1").Value)

                excel.Application.Run("FormMaker.xlsm!Module1.gaoxzTest")

                # 关闭Excel文件,不保存(若保存,使用True即可)
                # w1.Close(False)
                # w2.Close(False)

                # 退出Excel
                excel.Quit()
                pythoncom.CoUninitialize()

    input_path = target + "/input"  # 文件夹目录
    zip_path(target + "/output", target, "gaoxz.zip")

    if is_ajax:
        return ajax_response(True, upload_key)
    else:
        return redirect(url_for("upload_complete", uuid=upload_key))