You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从含8列的CSV提取数据并在MS Word中生成不同表格?

批量从CSV提取数据并生成Word表格的解决方案

方案1:Python自动化(灵活度最高)

适合需要自定义复杂筛选规则、批量生成多份表格的场景,几行代码就能搞定重复工作:

  1. 先安装依赖包,打开命令提示符输入:
    pip install pandas python-docx
  2. 示例代码(按需修改):
import pandas as pd
from docx import Document
from docx.shared import Pt

# 读取CSV文件,按需筛选数据
df = pd.read_csv("你的数据文件.csv")
# 示例1:提取指定列生成表格
filtered_data1 = df[["姓名", "部门", "业绩"]]
# 示例2:按条件筛选行,比如只保留业绩超1000的记录
filtered_data2 = df[df["业绩"] > 1000]

# 创建Word文档
doc = Document()

# 生成第一个表格
doc.add_heading("部门业绩汇总表", level=1)
table1 = doc.add_table(rows=1, cols=len(filtered_data1.columns))
# 设置表头样式
hdr_cells = table1.rows[0].cells
for i, col in enumerate(filtered_data1.columns):
    hdr_cells[i].text = col
    hdr_cells[i].paragraphs[0].runs[0].font.bold = True
# 填充数据行
for _, row in filtered_data1.iterrows():
    row_cells = table1.add_row().cells
    for i, val in enumerate(row):
        row_cells[i].text = str(val)

# 生成第二个表格(按需添加更多)
doc.add_page_break()
doc.add_heading("业绩达标人员表", level=1)
table2 = doc.add_table(rows=1, cols=len(filtered_data2.columns))
hdr_cells = table2.rows[0].cells
for i, col in enumerate(filtered_data2.columns):
    hdr_cells[i].text = col
    hdr_cells[i].paragraphs[0].runs[0].font.bold = True
for _, row in filtered_data2.iterrows():
    row_cells = table2.add_row().cells
    for i, val in enumerate(row):
        row_cells[i].text = str(val)

# 保存文档
doc.save("批量生成表格.docx")

修改代码里的列名、筛选条件,就能生成你需要的任意表格组合。

方案2:Excel Power Query + Word邮件合并(零编程)

适合不想写代码的用户,用Office自带工具就能完成:

  • 第一步:打开Excel,点击「数据」选项卡 → 「获取数据」→ 「从文件」→ 「从CSV」,导入你的CSV文件。用Power Query编辑器筛选、整理数据,比如拆分出不同的数据集,分别保存为独立的工作表。
  • 第二步:打开Word,点击「邮件」选项卡 → 「开始邮件合并」→ 「邮件合并分步向导」,选择「目录」类型,选中刚才的Excel文件作为数据源。插入表格并选择对应的数据区域,完成合并后就能生成包含目标表格的Word文档。

方案3:Word VBA(直接在Word内操作)

如果你熟悉VBA,可以直接在Word里写代码读取CSV并生成表格:

Sub CSVToWordTable()
    Dim csvPath As String
    Dim fileNum As Integer
    Dim lineContent As String
    Dim dataArray As Variant
    Dim targetTable As table
    Dim currentRow As Integer, colIndex As Integer
    
    ' 替换成你的CSV文件路径
    csvPath = "C:\你的数据.csv"
    fileNum = FreeFile()
    Open csvPath For Input As #fileNum
    
    ' 创建初始表格(8列对应你的CSV列数)
    Set targetTable = ActiveDocument.tables.Add(Selection.Range, 1, 8)
    currentRow = 1
    
    Do Until EOF(fileNum)
        Line Input #fileNum, lineContent
        ' 注意:如果CSV用分号分隔,把","改成";"
        dataArray = Split(lineContent, ",")
        
        ' 设置表头样式
        If currentRow = 1 Then
            For colIndex = 0 To UBound(dataArray)
                targetTable.Cell(currentRow, colIndex + 1).Range.Text = dataArray(colIndex)
                targetTable.Cell(currentRow, colIndex + 1).Range.Font.Bold = True
            Next colIndex
        Else
            ' 添加新行并填充数据
            targetTable.Rows.Add
            currentRow = currentRow + 1
            For colIndex = 0 To UBound(dataArray)
                targetTable.Cell(currentRow, colIndex + 1).Range.Text = dataArray(colIndex)
            Next colIndex
        End If
    Loop
    
    Close #fileNum
    ' 可选:套用表格样式
    targetTable.AutoFormat wdTableFormatList1
End Sub

打开Word的开发者选项卡,插入模块粘贴代码,修改CSV路径后运行即可。如果要生成多个表格,复制代码块修改筛选逻辑重复执行就行。

内容的提问来源于stack exchange,提问作者Bigefex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:00:20