Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从循环中的当前工作表获取sheetname_Python_Openpyxl - Fatal编程技术网

在Python中从循环中的当前工作表获取sheetname

在Python中从循环中的当前工作表获取sheetname,python,openpyxl,Python,Openpyxl,我想编写一个python代码,读取当前工作目录中的所有xlsx文件,并将它们作为CSV文件输出 单个Excel xlsx文件包含多个工作表,因此我必须为每个工作表创建一个CSV文件。CSV的文件名应为.CSV 我的代码是: import os import openpyxl import csv for excelFile in os.listdir('.'): if excelFile.endswith('.xlsx'): wb = openpyxl.load_w

我想编写一个python代码,读取当前工作目录中的所有xlsx文件,并将它们作为CSV文件输出

单个Excel xlsx文件包含多个工作表,因此我必须为每个工作表创建一个CSV文件。CSV的文件名应为.CSV

我的代码是:

import os
import openpyxl
import csv  

for excelFile in os.listdir('.'):
    if excelFile.endswith('.xlsx'):
        wb = openpyxl.load_workbook(excelFile)
        for sheet in wb:
            sheetname=sheet.title
            csvFileName = open(excelFile +"-"+ sheetname+ '.csv', 'w', newline='')
            csvFile = csv.writer(csvFileName)
            for rowNum in range(1, sheet.max_row + 1):
                rowData = []
                for colNum in range(1, sheet.max_column + 1):
                    cellData = sheet.cell(row=rowNum, column=colNum).value
                    rowData.append(cellData)
                csvFile.writerow(rowData)
            csvFileName.close()
它给出了我想要的,但我唯一不明白的是输出名称是:example.xlsx-sheet1.csv。但是我不想在这里显示.xlsx,所以我想要的是没有.xlsx的文件名,因此结果是example-sheet1.cvs


那么,我应该对当前代码做什么更改呢?

打开新文件时,只需从
excelFile
中删除扩展名即可:

import os
import openpyxl
import csv  

for excelFile in os.listdir('.'):

    name, extension = os.path.splitext(excelFile)

    if extension == 'xlsx':
        wb = openpyxl.load_workbook(excelFile)
        for sheet in wb:
            sheetname=sheet.title
            csvFileName = open('{}-{}.csv'.format(name, sheetname), 'w', newline='')
            csvFile = csv.writer(csvFileName)
            for rowNum in range(1, sheet.max_row + 1):
                rowData = []
                for colNum in range(1, sheet.max_column + 1):
                    cellData = sheet.cell(row=rowNum, column=colNum).value
                    rowData.append(cellData)
                csvFile.writerow(rowData)
            csvFileName.close()

另一方面,您应该尝试避免使用mixedCase约定命名变量,并坚持使用小写和下划线。我建议您仔细阅读PEP8指南,更好地了解常见的命名约定。

打开新文件时,只需从
excelFile
中删除扩展名即可:

import os
import openpyxl
import csv  

for excelFile in os.listdir('.'):

    name, extension = os.path.splitext(excelFile)

    if extension == 'xlsx':
        wb = openpyxl.load_workbook(excelFile)
        for sheet in wb:
            sheetname=sheet.title
            csvFileName = open('{}-{}.csv'.format(name, sheetname), 'w', newline='')
            csvFile = csv.writer(csvFileName)
            for rowNum in range(1, sheet.max_row + 1):
                rowData = []
                for colNum in range(1, sheet.max_column + 1):
                    cellData = sheet.cell(row=rowNum, column=colNum).value
                    rowData.append(cellData)
                csvFile.writerow(rowData)
            csvFileName.close()
另一方面,您应该尝试避免使用mixedCase约定命名变量,并坚持使用小写和下划线。我建议您阅读PEP8指南,更好地了解常见的命名约定-