【问题标题】:VBA Macro not working with CSV fileVBA 宏不适用于 CSV 文件
【发布时间】:2018-08-06 15:41:15
【问题描述】:

我需要能够在 150 个 csv 文件上运行一个宏,该宏可以双击工作表中的每个非空单元格。我需要这个,因为我拥有的文件包含 Excel 无法识别的自定义日期格式,除非我双击单元格。因为我有 150 个工作表,每个工作表都有数千列和行,所以我需要从 python 注入一个宏,因为我使用的是 python。要注入宏,我使用的是从另一篇文章 (Use Python to Inject Macros into Spreadsheets) 复制的以下代码;我做了一些修改以适应我的设置。 问题是,如果我在 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

关于如何使宏工作的任何想法?

【问题讨论】:

  • 我需要这个,因为我拥有的文件包含自定义日期格式,除非我双击单元格,否则 Excel 无法识别。 你确定吗?但是如果你双击 Excel 确实可以识别它,对吗?而且您不必双击它,只需将范围分配给自身,例如range.value = range.value。
  • @Storax 是的,如果我双击单元格,则 excel 会识别它并重新格式化。如果我制作一个主体为 range.value = range.value 的 vba 宏,那么我的问题将得到解决吗?
  • 我猜但没有任何示例数据很难分辨

标签: python python-3.x excel vba csv


【解决方案1】:

我通常使用 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

【讨论】:

  • 对不起,我错过了 python 的部分......这是一个 excel vba 代码,而不是 python。
  • 所以我没有运行那个 vba 代码,但我手动使用了 TextToColumns 函数来获取人类可读的信息。例如,我拥有的数据格式为“mm/dd/yy HH:mm:ss:”,然后它被分成两列,一列像“mm/dd/yyyy”,另一列是时间。但是,文本位于 excel 文件的中间并覆盖我不希望发生的相邻列。
  • 使用stackoverflow.com/questions/17684610/python-convert-csv-to-xlsx 此处的代码并使用编码 ISO-8859-1 将 csv 文件转换为 xlsx 文件解决了格式问题。
猜你喜欢
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多