将多个 CSV 转换为 xls 或 xlsx 并对其应用格式

发布于 2025-01-08 20:53:04 字数 584 浏览 1 评论 0原文

我有一个很大的 Excel 文件,用于聚合多个客户端的问题跟踪,我需要根据客户端将其分成更小的文件。

到目前为止,我已经得到了以下内容:我构建了一个 python 脚本,该脚本将文件划分为适当的特定于客户端的文件,并将正确的标题行应用于所有文件,但它生成的 CSV 是很丑。我的老板想要应用一些格式,所以我需要做的是(对于目录中的所有 CSV):

  1. 将每个文件从 CSV 转换为 .xls(或 .xlsx),以便它将保留格式

  2. 自动调整列宽度

  3. 将第一行设为粗体(并且可能应用颜色)到row)

到目前为止,我猜我可以使用 pyWin32pyExcelerator,但考虑如何我的改变最小想要制作一个 VB 脚本或宏或其他东西可能更容易做到这一点,但我真的不知道这些工具。

I have a large excel file that is used to aggregate tracking for issues across multiple clients and I need to divide it into smaller files based on the clients.

Here's what I've got so far: I've built a python script that divides the file into the appropriate client-specific files and applies the correct header row to all of them, but it produces CSVs which are ugly. My boss wants to apply some formatting, so what I need to do is (for all CSVs in the directory):

  1. convert each file from CSV to .xls (or .xlsx) so it will hold the formatting

  2. Auto-fit column widths

  3. Make the first row bold (and perhaps apply a color to the row)

So far I'm guessing I can probably do this with either pyWin32 or pyExcelerator, but considering how minimal the changes I'm looking to make are it may be easier to do this as a VB script or a macro or something, but I don't really know those tools.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2025-01-15 20:53:04

最简单的方法是使用 VBA

这是一个快速宏,用于循环打开 csv 的文件夹,应用自动调整并另存为 .xlsx

注意:

  1. 它使用早期绑定,因此需要对脚本运行时的引用。如果您愿意,可以轻松更改为后期绑定。
  2. 为了清楚起见,我省略了错误处理和各种速度优化。它是否足够安全或足够快将取决于您的预期用途以及文件的数量和大小。

Sub FormatCSVs()
    Dim fso As FileSystemObject
    Dim pth As String
    Dim fl As File
    Dim wb As Workbook
    
    Set fso = New FileSystemObject
    pth = "C:\Test" ' <-- replace with your path, or add a folder selection dialog>
    For Each fl In fso.GetFolder(pth).Files
       If StrComp(fso.GetExtensionName(fl.Path), "csv", vbTextCompare) = 0 Then
            Set wb = Workbooks.Open(fl.Path)
            With wb.Sheets(1)
                .UsedRange.EntireColumn.AutoFit
                .Rows(1).Font.Bold = True
                .Rows(1).Interior.ColorIndex = 3
            End With
            wb.SaveAs pth & "\" & fso.GetBaseName(fl.Path), xlOpenXMLWorkbook
               ' or use xlExcel8 for .xls
            wb.Close
        End If
    Next
    
    Set fl = Nothing
    Set fso = Nothing
End Sub

The simplest way would be to use VBA

Here's a quick macro to loop thriugh a folder of csv's open, apply autofit and save as .xlsx

Note:

  1. It utilises early binding, therefore requires a reference to the Scripting Runtime. Can easily be altered to late binding if you prefer.
  2. For clarity I have ommitted error handling and various speed optimisations. Whether it's safe enough or fast enough will depend on your intended use and number and size of files.

Sub FormatCSVs()
    Dim fso As FileSystemObject
    Dim pth As String
    Dim fl As File
    Dim wb As Workbook
    
    Set fso = New FileSystemObject
    pth = "C:\Test" ' <-- replace with your path, or add a folder selection dialog>
    For Each fl In fso.GetFolder(pth).Files
       If StrComp(fso.GetExtensionName(fl.Path), "csv", vbTextCompare) = 0 Then
            Set wb = Workbooks.Open(fl.Path)
            With wb.Sheets(1)
                .UsedRange.EntireColumn.AutoFit
                .Rows(1).Font.Bold = True
                .Rows(1).Interior.ColorIndex = 3
            End With
            wb.SaveAs pth & "\" & fso.GetBaseName(fl.Path), xlOpenXMLWorkbook
               ' or use xlExcel8 for .xls
            wb.Close
        End If
    Next
    
    Set fl = Nothing
    Set fso = Nothing
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文