简体   繁体   English

如何粘贴到 Openpyxl 中的活动单元格

[英]How to Paste to Active Cell in Openpyxl

How is active cell set with the current openpyxl version?当前的 openpyxl 版本如何设置活动单元格?

Charlie Clark Ref: https://stackoverflow.com/a/36763368/4539999 - doesn't work.查理克拉克参考: https ://stackoverflow.com/a/36763368/4539999 - 不起作用。

It's either the wrong functionality or a bug.这是错误的功能或错误。 In excel if a cell is selected then a paste is performed paste occurs at the active cell (similar to the active sheet).在 excel 中,如果选择了一个单元格,则在活动单元格处执行粘贴(类似于活动工作表)。 Openpyxl seems to ignore active cell. Openpyxl 似乎忽略了活动单元格。

import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
#ws.sheet_view.selection[0].activeCell = 'B4'
#ws.sheet_view.selection[0].sqref = 'B4'
ws.views.sheetView[0].selection[0].activeCell = 'B4'
#c = ws['B4']
cell=(['Hello'])
ws.append(cell)
wb.save('Test.xlsx')

Output输出

在此处输入图像描述

Question notes excel pastes into active cell, the ws.append demonstrates number of rows in sheet is zero and image demonstrates an active cell in sheet.问题注释 excel 粘贴到活动单元格中,ws.append 显示工作表中的行数为零,图像显示工作表中的活动单元格。

It appears writing to active cell in openpyxl requires active cell to be specifically addressed and in excel it is inferred.似乎在openpyxl中写入活动单元格需要专门处理活动单元格,并且在excel中推断它。

import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
# set active cell for demo
ws.views.sheetView[0].selection[0].activeCell = 'B4'
# set value of active cell
ws[ws.views.sheetView[0].selection[0].activeCell] = 'Hello'
wb.save('Test.xlsx')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM