简体   繁体   English

从csv中的单元格范围复制到Excel错误

[英]Copying from range of cells in csv to excel error

I am migrating from vba to python and am trying to copy a range of columns 2-11 to an existing workbook 2-11. 我正在从vba迁移到python,并尝试将2-11列的范围复制到现有工作簿2-11。 It tends to work other than throw a: 它趋向于工作,而不是抛出:

Exception: Invalid Excel character '[]:*?/\\' in sheetname

Python code : Python代码:

import os
import glob
import csv
from xlsxwriter.workbook import Workbook
workbook = Workbook('C:/Users/AWA/AA1.xlsx')

for csvfile in glob.glob(os.path.join('C:/AWA/AA.csv')):
    worksheet = workbook.add_worksheet(os.path.splitext(csvfile)[0]) 
    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) 
    workbook.close()

Don't use so low level method. 不要使用如此低级的方法。 I recommend using pandas for such migration: 我建议使用熊猫进行此类迁移:

data=pandas.read_excel('myfile.xlsx',sheetname=None)
fa=data["Sheet 1"]

All your sheets will become arrays in one instruction. 您所有的工作表将在一条指令中变成数组。 for that you must install pandas and xlrd . 为此,您必须安装pandasxlrd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM