[英]Python - Extracting excel docs from file, need help reading data
因此,我一直在一個項目中嘗試從文件中提取.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()
運行程序時收到的錯誤
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,但是即使將模塊相應地安裝,也無法將模塊導入我的外殼。
任何提示都會有所幫助!
謝謝!
它是WorkBook
的大寫W
wb = xlsxwriter.Workbook('merged.xls')
您還應該在Windows的路徑中使用/
斜杠或r
原始字符串:
r'C:\Users\username\Desktop\Mults'
'C:/Users/username/Desktop/Mults'
ws = workbook.add_worksheet()
也將導致錯誤,因為未在任何地方定義workbook
。
我想你的意思是wb.add_worksheet()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.