Linux 上的 xlwings 替代方案
xlwings alternative on Linux
我有一个小的 Python 脚本,目的是更新存储在 Onedrive 上的 Excel sheet 的某些单元格。特别是,Excel 工作簿在一个 sheet 中包含价目表,在第二个 sheet,以及第 3 个货币汇率的历史记录(也将通过 Python 进行编辑)。我想让 Raspberry Pi 运行 并在正常时间执行脚本,从而更新包含从兰特到欧元的货币汇率的字段,并将当前值插入货币汇率的历史记录.
如果 Excel sheet 在其他设备上打开等(多个用户应该能够读取和编辑文件),脚本应该可以正常工作而不会导致错误。
在 Windows,我是这样工作的:
from forex_python.converter import CurrencyRates
import xlwings as xw
from datetime import datetime
# get date and time
now = datetime.now()
current_date = now.strftime("%Y-%m-%d")
current_time = now.strftime("%H:%M:%S")
# Get current Rand to Euro conversion rate
rates = CurrencyRates()
RtoEUR = rates.get_rate("ZAR", "EUR")
# edit workbook
exl_app = xw.App(visible = False)
wb = xw.Book("/mnt/usb/LELE_expenses.xlsx")
## update currency exch. rate
sht0 = wb.sheets["Auto_upd_vals"]
sht0.range("C2").value = RtoEUR
sht1 = wb.sheets["Hist"]
last = sht1.range("B" + str(wb.sheets[0].cells.last_cell.row)) \
.end("up").row
## save current date, time and currency exch. rate
sht1.range("A" + str(last + 1)).value = current_date
sht1.range("B" + str(last + 1)).value = current_time
sht1.range("C" + str(last + 1)).value = RtoEUR
wb.save()
wb.close()
exl_app.quit()
很遗憾,无法在 Raspbian 上安装 xlwings
。有没有其他方法可以实现这一目标?
到目前为止的错误:
pip3 install xlwings
导致“xlwings 需要安装 Excel,因此仅适用于 Windows 和 macOS...”
运行 export INSTALL_ON_LINUX=1
启用安装,但无法导入模块。大概是因为它还没有 Excel.
嗯,我想这比我预期的要容易:
from forex_python.converter import CurrencyRates
from openpyxl import Workbook, load_workbook
from datetime import datetime
# get date and time
now = datetime.now()
current_date = now.strftime("%Y-%m-%d")
current_time = now.strftime("%H:%M:%S")
# Get current Rand to Euro conversion rate
rates = CurrencyRates()
RtoEUR = rates.get_rate("ZAR", "EUR")
# edit workbook
wb = load_workbook("/mnt/usb/LELE_expenses.xlsx")
## update currency exchange rate
sht0 = wb["Auto_upd_vals"]
sht0["C2"].value = RtoEUR
# append currency exchange rate history
sht1 = wb["Hist"]
sht1.append([current_date, current_time, RtoEUR])
wb.save("/mnt/usb/LELE_expenses.xlsx")
wb.close()
不需要 Excel。不过,我不确定它是否会引起任何问题。
我有一个小的 Python 脚本,目的是更新存储在 Onedrive 上的 Excel sheet 的某些单元格。特别是,Excel 工作簿在一个 sheet 中包含价目表,在第二个 sheet,以及第 3 个货币汇率的历史记录(也将通过 Python 进行编辑)。我想让 Raspberry Pi 运行 并在正常时间执行脚本,从而更新包含从兰特到欧元的货币汇率的字段,并将当前值插入货币汇率的历史记录. 如果 Excel sheet 在其他设备上打开等(多个用户应该能够读取和编辑文件),脚本应该可以正常工作而不会导致错误。 在 Windows,我是这样工作的:
from forex_python.converter import CurrencyRates
import xlwings as xw
from datetime import datetime
# get date and time
now = datetime.now()
current_date = now.strftime("%Y-%m-%d")
current_time = now.strftime("%H:%M:%S")
# Get current Rand to Euro conversion rate
rates = CurrencyRates()
RtoEUR = rates.get_rate("ZAR", "EUR")
# edit workbook
exl_app = xw.App(visible = False)
wb = xw.Book("/mnt/usb/LELE_expenses.xlsx")
## update currency exch. rate
sht0 = wb.sheets["Auto_upd_vals"]
sht0.range("C2").value = RtoEUR
sht1 = wb.sheets["Hist"]
last = sht1.range("B" + str(wb.sheets[0].cells.last_cell.row)) \
.end("up").row
## save current date, time and currency exch. rate
sht1.range("A" + str(last + 1)).value = current_date
sht1.range("B" + str(last + 1)).value = current_time
sht1.range("C" + str(last + 1)).value = RtoEUR
wb.save()
wb.close()
exl_app.quit()
很遗憾,无法在 Raspbian 上安装 xlwings
。有没有其他方法可以实现这一目标?
到目前为止的错误:
pip3 install xlwings
导致“xlwings 需要安装 Excel,因此仅适用于 Windows 和 macOS...”
运行 export INSTALL_ON_LINUX=1
启用安装,但无法导入模块。大概是因为它还没有 Excel.
嗯,我想这比我预期的要容易:
from forex_python.converter import CurrencyRates
from openpyxl import Workbook, load_workbook
from datetime import datetime
# get date and time
now = datetime.now()
current_date = now.strftime("%Y-%m-%d")
current_time = now.strftime("%H:%M:%S")
# Get current Rand to Euro conversion rate
rates = CurrencyRates()
RtoEUR = rates.get_rate("ZAR", "EUR")
# edit workbook
wb = load_workbook("/mnt/usb/LELE_expenses.xlsx")
## update currency exchange rate
sht0 = wb["Auto_upd_vals"]
sht0["C2"].value = RtoEUR
# append currency exchange rate history
sht1 = wb["Hist"]
sht1.append([current_date, current_time, RtoEUR])
wb.save("/mnt/usb/LELE_expenses.xlsx")
wb.close()
不需要 Excel。不过,我不确定它是否会引起任何问题。