我是一个新手,试图编写一个脚本来从电子表格中提取信息,并使用autopygui和openpyxl将其作为报告呈现在Libre办公室作家中。我的尝试:
from subprocess import call
import openpyxl, pyautogui
call("libreoffice")
calcfile = "calcexample.xlsx"
pyautogui.hotkey('alt', 'tab') # focus on Writer
wb = openpyxl.load_workbook(calcfile) # open xml file
sheet = wb.get_sheet_by_name('Sheet1') # open sheet X, to print specific cells
print = ("Reading calc file...")
# trying to write cells as text in the doc
for row in range(2, sheet.max_row):
for col in range (2, 6):
pyautogui.typewrite(sheet[col, row].value)
pyautogui.typewrite('enter')
我收到此错误:
> Traceback (most recent call last): File "a.output.py", line 30, in
> <module><br>
> pyautogui.typewrite(sheet[col, str(row)].value) File "/home/m/.local/lib/python3.5/site-packages/openpyxl/worksheet/worksheet.py",
> line 354, in __getitem__<br>
> min_col, min_row, max_col, max_row = range_boundaries(key) File "/home/m/.local/lib/python3.5/site-packages/openpyxl/utils/cell.py",<br>
> line 127, in range_boundaries<br>
> m = ABSOLUTE_RE.match(range_string) TypeError: expected string or bytes-like object<br>
> m@m-SATELLITE-C855-2CQ:~/python-stuff/projects/04-Asistente
> corrección$ python3 a.output.py Traceback (most recent call last):
> File "a.output.py", line 30, in <module><br>
> pyautogui.typewrite(sheet[str(col), str(row)].value) File "/home/m/.local/lib/python3.5/site-packages/openpyxl/worksheet/worksheet.py",
> line 354, in __getitem__<br>
> min_col, min_row, max_col, max_row = range_boundaries(key) File "/home/m/.local/lib/python3.5/site-packages/openpyxl/utils/cell.py",
> line 127, in range_boundaries<br>
> m = ABSOLUTE_RE.match(range_string) <br> TypeError: expected string or bytes-like object<br>
我认为这是由于“col”和“row”的格式。我尝试使用str()没有结果。我知道我在这里遗漏了一些非常基本的东西,但无法弄清楚是什么。我的问题:
答案 0 :(得分:0)
单元格不应作为sheet[col, row]
访问。你为什么这么认为?正如tutorial所说,使用指定位置的单个字符串:
sheet['A4']
或使用cell
方法:
sheet.cell(row=row, column=col)