简体   繁体   English

用xlsxwriter python编写多个Excel文件

[英]write multiple excel files with xlsxwriter python

is there a way to create multi excel files with xlsxwriter? 有没有办法用xlsxwriter创建多个Excel文件?

from itertools import chain
import glob ,csv, sys, os

openSoundingFile = 'D:/apera/Workspace/Sounding2/*.txt'


for filename in glob.glob(openSoundingFile):
    newName = filename
    spamReader = csv.reader(open(filename, 'rb'), delimiter=';',quotechar='"')
    workbook = xlsxwriter.Workbook('D:/apera/Workspace/Sounding2/' + newName[:-4] + '.xlsx'
    sheet = workbook.add_worksheet('Original data')

    for rowx, row in enumerate(spamReader):
        for colx, value in enumerate(row):
            sheet.write(rowx, colx, value)


    workbook.close()

so i wanna to save all the txt files to exel files. 所以我想将所有的txt文件保存为exel文件。 I think the problem is here 我认为问题出在这里

workbook = xlsxwriter.Workbook('D:/apera/Workspace/Sounding2/' + newName[:-4] + '.xlsx'

if i'm not using + newName[:-4] + it will work but only write 1 excel files. 如果我不使用+ newName[:-4] +它将起作用,但只能写入1个excel文件。 Is there a way to do it? 有办法吗?

The error showed that you combined the path to the files 2 times on top of each other: 该错误表明,您将文件的路径相互叠加了2次:

'D:/apera/Workspace/Sounding2/bla.txtD:/apera/Workspace/Sounding2/'

This does it for me: 这为我做到了:

import xlsxwriter
import glob ,csv

openSoundingFile = 'D:/apera/Workspace/Sounding2/*.txt'

for filename in glob.glob(openSoundingFile):
    spamReader = csv.reader(open(filename, 'rb'), delimiter=';',quotechar='"')
    # Note that filename is the full path already! Just [:-4] to remove .txt
    workbook = xlsxwriter.Workbook(filename[:-4] + '.xlsx')
    sheet = workbook.add_worksheet('Original data')
    for rowx, row in enumerate(spamReader):
        for colx, value in enumerate(row):
            sheet.write(rowx, colx, value)
    workbook.close()

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

相关问题 Python使用XlsxWriter将多个矩阵写入excel - Python write multiple matrix into excel using XlsxWriter 迭代多个Dataframe并在excel xlsxwriter python中写入excel电子表格 - Iterate multiple Dataframe and write into excel spreadsheets within an excel xlsxwriter python 在while循环中使用xlsxwriter将多个公式写入excel工作表 - Write multiple formulas to excel sheet with xlsxwriter in while loop 如何使用 xlsxwriter 模块在多个单元格中的 excel 中动态编写公式? - How to write formula dynamically in excel in multiple cells using xlsxwriter module? 如何使用“xlsxwriter”而不是“openpyxl”写入具有多个工作表的 excel? - How to write to an excel with multiple worksheets using “xlsxwriter” and not “openpyxl”? 使用 pandas xlsxwriter 写入 excel 表中的多行 - Write to multiple rows in excel sheet using pandas xlsxwriter 在 python 中使用.write_formula (xlsxwriter) 将 excel 中的 2 列相乘 - Multiplying 2 columns in excel using .write_formula (xlsxwriter) in python 使用Python xlsxwriter模块将srt数据写入Excel - Use Python xlsxwriter module to write srt data into and excel 使用 Python 和 XLSXWriter 将格式应用于多个 Excel 工作表 - Applying formatting to multiple Excel sheets using Python and XLSXWriter 如何将Python数据框写入多个Excel文件的多个表 - How to write Python dataframe to multiple sheets of multiple Excel files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM