使用python将.csv文件复制到.xlsx工作簿中 [英] Copy .csv files into a .xlsx workbook using python

查看:633
本文介绍了使用python将.csv文件复制到.xlsx工作簿中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进行了广泛的研究,但是未能找到满足我需求的正确解决方案.

I have done extensive researches, however have not been able to come up with the right solution for my needs.

我有python脚本,可以生成多个.csv文件.

I have python script which generates multiple .csv files.

每个.csv文件仅在A列和&列中包含数据. B.

Each .csv file only has data in column A & B.

每个.csv文件都有一个唯一的名称,即时通讯试图找出如何根据其名称将.csv文件复制到现有的excel工作簿中,并复制到具有相同名称的特定标签/工作表中.

Each .csv file has a unique name, and im trying to work out how to copy the .csv file based on its name, into an existing excel workbook, into a specific tab/sheet of the same name.

.csv文件将始终位于同一文件夹中.

The .csv files will always be in the same folder.

理想情况下,我想使用python来完成此任务.

I would ideally like to use python for this task.

推荐答案

您可以尝试这样的事情

import os
import glob
import csv
from xlsxwriter.workbook import Workbook
workbook = Workbook('Existing.xlsx')

for csvfile in glob.glob(os.path.join('.', '*.csv')):
    worksheet = workbook.add_worksheet(os.path.splitext(csvfile)[0]) # worksheet with csv file name
    with open(csvfile, 'rb') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col) # write the csv file content into it
 workbook.close()

这篇关于使用python将.csv文件复制到.xlsx工作簿中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆