因此,我一直致力於從文件中提取.xlsx文檔以嘗試將數據編譯到一個工作表中。Python - 從文件中提取excel文檔,需要幫助讀取數據
因此,我已經管理了一個循環來拉文件,但現在我卡住試圖閱讀文件。
的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()
腳本和響應,當我運行程序
C:\Users\username\Desktop\Mults\doc1.xlsx
C:\Users\username\Desktop\Mults\doc2.xlsx
C:\Users\username\Desktop\Mults\doc3.xlsx
C:\Users\username\Desktop\Mults\doc4.xlsx
Traceback (most recent call last):
File "C:\Users\username\Desktop\Work\Python\excel practice\xlsx - loops files
- 09204.py", line 20, in <module>
wb = xlsxwriter.workbook('merged.xls')
TypeError: 'module' object is not callable
我知道我是我收到的錯誤錯過了某個連接數據的步驟。
我已經在xlsxwriter中練習過其他腳本,並且模塊工作正常。出於某種原因在這裏不會識別它。
此外,建議我試過xlwt,但遇到麻煩導入模塊到我的外殼,即使它相應地安裝。
任何提示將有所幫助!
謝謝!
不知道爲什麼你不斷翻滾你的問題來回。你能解釋一下嗎? – 2014-09-29 20:46:29