2022年6月6日 星期一

在樹莓派上測試 openpyxl 與 xlwings 套件

上周四與今天兩天參加公司 Python 與 Excel 整合操作內訓, 收穫頗豐, 老師介紹了一款可操作 Excel 檔案的 Python 套件 xlwings, 比我之前學過一些皮毛的 openpyxl 還好用, 主要有兩點 :
  • 儲存格與工作表操作即時反應在 Excel, 不需要像 openpyxl 呼叫 save() 存檔
  • 即使 Excel 檔案在開啟狀態中也可以被程式同時存取, 不需要關檔
但 xlwings 有一個缺點, 它必須在有安裝微軟 Excel 軟體的環境下才能使用, 而 openpyxl 則無此限制. 下課後我就迫不及待要來驗證一下是否 openpyxl 與 xlwings 可在無法安裝微軟 Excel 的樹莓派上執行, 以下參考之前在 Win 10 上的測試跑一遍 :


我先把測試用的 Excel 檔案 users.xlsx 用 WinSCP 軟體 FTP 傳送到樹莓派的使用者目錄 home/pi 底下, 這是以下 Python 測試的工作目錄 :




1. 安裝 openpyxl :   

pi@raspberrypi:~ $ pip3 install openpyxl    
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting openpyxl
  Downloading https://files.pythonhosted.org/packages/7b/60/9afac4fd6feee0ac09339de4101ee452ea643d26e9ce44c7708a0023f503/openpyxl-3.0.10-py2.py3-none-any.whl (242kB)
Collecting et-xmlfile (from openpyxl)
  Downloading https://files.pythonhosted.org/packages/96/c2/3dd434b0108730014f1b96fd286040dc3bcb70066346f7e01ec2ac95865f/et_xmlfile-1.1.0-py3-none-any.whl
Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.0.10


2. 用 openpyxl 載入 Excel 檔案與存取工作表 :   

pi@raspberrypi:~ $ python3    
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl as xl     
>>> type(xl)    
<class 'module'>
>>> wb=xl.load_workbook('users.xlsx')      
>>> type(wb)    
<class 'openpyxl.workbook.workbook.Workbook'>
>>> sheets=wb.sheetnames     
>>> sheets    
['工作表1', '我的工作表', 'Sheet2']
>>> ws=wb.worksheets      
>>> ws[0]   
<Worksheet "工作表1">
>>> ws[1]   
<Worksheet "我的工作表">
>>> ws[2]   
<Worksheet "Sheet2">
>>> ws[0].title    
'工作表1'
>>> wb.active   
<Worksheet "工作表1">
>>> wb.active.title 
'工作表1'

可見在沒有安裝 Excel 的 Linux 下可以順利使用 openpyxl. 


3. 安裝 xlwings :  

pi@raspberrypi:~ $ pip3 install xlwings    
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting xlwings
  Downloading https://files.pythonhosted.org/packages/e0/68/1a49b93e151415138afd5970c14f7b339f85944f4e34a37ce0efab8d7106/xlwings-0.27.9-py3-none-any.whl (1.0MB)
Installing collected packages: xlwings
Successfully installed xlwings-0.27.9


4. 用 xlwings 載入 Excel 檔案與存取工作表 :  

pi@raspberrypi:~ $ python3 
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlwings as xw   
>>> wb=xw.Book('users.xlsx')    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.7/site-packages/xlwings/main.py", line 820, in __init__
    for app in apps:
  File "/home/pi/.local/lib/python3.7/site-packages/xlwings/main.py", line 225, in __iter__
    for app in self.impl:
  File "/home/pi/.local/lib/python3.7/site-packages/xlwings/main.py", line 4965, in impl
    "Your platform only supports the "
xlwings.XlwingsError: Your platform only supports the instantiation via xw.Book(json=...)

可見 xlwings 雖然可以順利在 Linux 上安裝, 但如果沒有同時安裝微軟的 Excel 也是無法使用 (一定要微軟的 Excel 才行, 就算安裝 OpenOffice 也不行), 似乎是須先將 xlsx 檔轉成 JSON 檔 (這樣就失去使用 xlwings 的意義了). 本來我想既然老師說 xlwings 更好用, 那我是不是該拋棄 openpyxl 了呢? 從上面測試結果看來, openpyxl 還是值得繼續學, 因為我是使用低功耗, 可以 24 小時開機的樹莓派當自動化主機, 而不是用高耗電的 Windows  PC 啊!

沒有留言 :