Python从文件中提取excel文档,需要帮助读取d

2024-04-26 05:00:07 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在做一个项目,从一个文件中提取.xlsx文档,试图将数据编译成一个工作表。在

因为我已经尝试了一个循环来提取文档,但现在我却无法阅读文档。在

Python 2.7

下面,我在shell中的脚本和响应

#-------------- loop that pulls in files from folder--------------
import os

#create directory from which to pull the files
rootdir = 'C:\Users\username\Desktop\Mults'

for subdir, dir, files in os.walk(rootdir):
for file in files:
    print os.path.join(subdir,file)
#----------------------merge work books-----------------------

import xlrd
import xlsxwriter


wb = xlsxwriter.workbook('merged.xls')
ws = workbook.add_worksheet()
for file in filelist:
    r = xlrd.open_workbook(file)
    head, tail = os.path.split(file)
    count = 0
    for sheet in r:
        if sheet.number_of_rows()>0:
            count += 1
    for sheet in r:
        if sheet.number_of_rosw()>0:
            if count == 1:
                sheet_name = tail
            else:
                sheet_name = "%s_%s" (tail, sheet.name)
            new_sheet = wb.create_sheet(sheet_name)
            new_sheet.write_reader(sheet)
            new_sheet.close()
wb.close()

运行程序时收到的错误

^{pr2}$

我知道我遗漏了一个连接数据的步骤。在

我用xlsxtwriter在其他脚本中进行了练习,该模块运行良好。因为某种原因在这里不认识它。在

另外,正如建议的那样,我尝试了xlwt,但是在将模块导入shell时遇到了困难,尽管它是相应地安装的。在

任何提示都会有帮助!在

谢谢!在


Tags: namein文档importnewforifos
1条回答
网友
1楼 · 发布于 2024-04-26 05:00:07

它是WorkBook中的大写W

 wb = xlsxwriter.Workbook('merged.xls')

您还应该在windows的路径中使用/斜杠或r原始字符串:

^{pr2}$

ws = workbook.add_worksheet()也将导致错误,因为workbook未在任何地方定义。在

我想你是说wb.add_worksheet()

相关问题 更多 >