Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python VBA宏不使用CSV文件_Python_Python 3.x_Excel_Vba_Csv - Fatal编程技术网

Python VBA宏不使用CSV文件

Python VBA宏不使用CSV文件,python,python-3.x,excel,vba,csv,Python,Python 3.x,Excel,Vba,Csv,我需要能够在150个csv文件上运行宏,双击工作表中的每个非空单元格。我需要这个,因为我的文件包含一个自定义日期格式,除非双击单元格,否则excel无法识别该格式。因为我有150个工作表,每个工作表有数千列和数千行,所以我需要从python中插入一个宏,因为我使用的是python。为了注入宏,我使用了以下代码,这些代码是我从另一篇文章()复制的;我做了一些修改以适应我的设置。 问题是,如果我在excel中运行VBA代码,它会工作,但如果我从python中注入它,宏将什么也不做。我认为这与我如何将

我需要能够在150个csv文件上运行宏,双击工作表中的每个非空单元格。我需要这个,因为我的文件包含一个自定义日期格式,除非双击单元格,否则excel无法识别该格式。因为我有150个工作表,每个工作表有数千列和数千行,所以我需要从python中插入一个宏,因为我使用的是python。为了注入宏,我使用了以下代码,这些代码是我从另一篇文章()复制的;我做了一些修改以适应我的设置。 问题是,如果我在excel中运行VBA代码,它会工作,但如果我从python中注入它,宏将什么也不做。我认为这与我如何将文件从dat转换为csv有关。这是我使用的代码

for j in range(len(dat_names)):
    # to record elapsed runtime
    archive_member_cycler_start = time.time()
    archive_members_list.extract(dat_names[j],destination)
    with open('%s.dat' % dat_names[j][:-4], 'r', encoding='ISO-8859-1') as input_file:
        # remove | characters from the data
        newLines = []
        for line in input_file:
            newLine = [x.strip() for x in line.split('|')]
            # remove ^ characters from the data also; they are the last column
            newLine = newLine[:-3]
            newLines.append(newLine)
    with open('%s.csv' % dat_names[j][:-4], 'w', newline='') as output_file:
        file_writer = csv.writer(output_file)
        try:
            file_writer.writerows(newLines)
            archive_member_cycler_end = time.time()
            archive_member_cycler_run = archive_member_cycler_end-archive_member_cycler_start

            a = '%s: %s.dat converted to csv (%.3f)' % (newpath[i], dat_names[j][:-4], archive_member_cycler_run)
            print(a)
            other.append(a)
            # remove dat file because dont want it
            os.remove('%s.dat' % dat_names[j][:-4])

        except Exception as e:
            print(e)
            pfn = str(newpath[i]+': '+dat_names[j][:-4])
            archive_member_cycler_end = time.time()
            archive_member_cycler_run = archive_member_cycler_end-archive_member_cycler_start

            a = '%s: %s.dat converted to csv WITH ERROR (%.3f)\n' % (newpath[i], dat_names[j][:-4], archive_member_cycler_run);
            print(a)
            other.append(a)

            # compress the problematic dat file
            zipper = zipfile.ZipFile('%s.zip' % dat_names[j][:-4], 'w')
            zipper.write('%s.dat' % dat_names[j][:-4], compress_type=zipfile.ZIP_DEFLATED)
            zipper.close()
            # remove the dat file because dont want it
            os.remove('%s.dat' % dat_names[j][:-4])
            pass

关于如何使宏工作有什么想法吗

我通常使用TextToColumns函数

B列示例:

Columns("B:B").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True

我需要这个,因为我的文件包含一个自定义日期格式,除非双击单元格,否则excel无法识别该格式。你确定吗?但是如果你双击Excel,你会认出它,对吗?您不必双击它,只需将范围分配给它自己,就像
range.value=range.value
@Storax是的,如果我双击单元格,excel就会识别它并重新格式化。如果我制作一个vba宏,其主体是range.value=range.value,那么我的问题将得到解决?我想,但是没有任何难以说明的示例数据。很抱歉,我错过了python的部分…这是一个excel vba代码,不是python,所以我没有运行vba代码,但我手动使用了TextToColumns函数,该函数用于获取人类可读的信息。例如,我的数据格式为“mm/dd/yy HH:mm:ss:“然后它被分成两列,一列像‘mm/dd/yyyy’,另一列是时间。但是,文本位于Excel文件的中间,并覆盖了我不想发生的相邻列。使用这里的代码将CSV文件转换成XLSX文件,并使用编码ISO-859-1解决了格式问题。