Python中openpyxl如何使用iter_rows()?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!
當我們使用以下代碼:
import openpyxl as op ms = op.load_workbook('mtest.xlsx') ws = ms.active op.worksheet.Worksheet.iter_rows()
然后會出現,此代碼返回:
type object 'Worksheet' has no attribute 'iter_rows'
怎么會出現這種情況?
這說明,您需要在工作表的實例上調用iter_rows方法,例如:
>>> for row in ws.iter_rows('A1:C2'): ... for cell in row: ... print cell
要么
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2): ... for cell in row: ... print(cell)
正如您的錯誤消息所述,您在Worksheet類型上調用它,這將無效;它需要在一個對象上調用:
op.worksheet.Worksheet.iter_rows() # wrong
對于舊版本的openpyxl,您可能需要確保在加載工作簿時啟用迭代器 –對于更新版本,這不是必需的。
以下是一個完整的例子在Python REPL中測試過(使用openpyxl 1.8.3):
>>> import openpyxl as op >>> wb = op.load_workbook('/tmp/test.xlsx', use_iterators=True) >>> ws = wb.active >>> for row in ws.iter_rows(): ... for cell in row: ... print cell ... RawCell(row=1, column='A', coordinate='A1', internal_value=1.0, data_type='n', style_id='0', number_format='general') RawCell(row=1, column='B', coordinate='B1', internal_value=10.0, data_type='n', style_id='0', number_format='general') ...
感謝各位的閱讀!看完上述內容,你們對Python中openpyxl如何使用iter_rows()大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注創新互聯-成都網站建設公司行業資訊頻道。
文章題目:Python中openpyxl如何使用iter_rows()-創新互聯
新聞來源:http://m.kartarina.com/article20/dchsco.html
成都網站建設公司_創新互聯,為您提供網站維護、Google、營銷型網站建設、靜態網站、域名注冊、用戶體驗
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯