1. 程式人生 > >Python3 安裝 xlutils

Python3 安裝 xlutils

說明 xlutils(excel utilities)是一個提供了許多操作修改excel檔案方法的庫。xlrd庫僅用於讀取excel檔案中的資料,xlwt庫則用於將資料寫入excel檔案,但是對於已有的excel檔案,想要追加或者修改,這兩個庫則沒有辦法完成。事實上也確實沒有什麼好辦法,至少目前來講還沒有。這裡將要講到的xlutils庫也僅僅是通過複製一個副本進行操作後儲存一個新檔案,xlutils庫就像是xlrd庫和xlwt庫之間的一座橋樑,因此,xlutils庫是依賴於xlrd和xlwt兩個庫的。 xlutils庫中包含了許多模組,這裡主要講最常使用的xlutils.copy模組。下面按照操作步驟,介紹其使用方法。 首先使用xlrd開啟檔案:

from os.path import join
from xlrd import open_workbook
rb = open_workbook(join(text_files,'testall.xls'))

然後使用xlutils.copy模組將xlrd.Book物件拷貝為一個xlwt.workbook物件(體現了xlutils庫橋樑的作用):

from xlutils.copy import copy
wb = copy(rb)

再使用xlwt的方法操作xlwt.workbook物件:

wb.get_sheet(0).write(0,0,'changed')
wb.save(join(temp_dir,'output.xls'))

儲存的檔案可以通過xlrd讀取。

pip3 install xlutils Collecting xlutils Downloading https://files.pythonhosted.org/packages/c7/55/e22ac73dbb316cabb5db28bef6c87044a95914f713a6e81b593f8a0d2f79/xlutils-2.0.0-py2.py3-none-any.whl (55kB) 100% |████████████████████████████████| 61kB 136kB/s Requirement already satisfied: xlwt>=0.7.4 in c:\python37\lib\site-packages (from xlutils) (1.3.0) Requirement already satisfied: xlrd>=0.7.2 in c:\python37\lib\site-packages (from xlutils) (1.1.0) Installing collected packages: xlutils The script margins.exe is installed in ‘c:\python37\Scripts’ which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed xlutils-2.0.0 You are using pip version 10.0.1, however version 18.1 is available. You should consider upgrading via the ‘python -m pip install --upgrade pip’ command.