我正在使用Python和openpyxl库,但是,当我的电子表格处于write_only=True
模式时,我不能在openpyxl中使用insert_cols()
函数。所以,基本上,我只想在电子表格处于write_only=True
模式时添加一个新列。
当通过load_workbook()
加载工作簿时,我可以使用insert_cols()
,但是当我使用write_only
模式时,就不行了。我必须使用write_only
模式,因为我的电子表格很大。你知道吗
任何关于如何添加一个新的专栏的想法都将不胜感激。你知道吗
谢谢你。你知道吗
这是我的代码:
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook(filename=r'path\myExcel.xlsx', read_only=True)
ws = wb['PC Details']
wb_output = Workbook(write_only=True)
ws_output = wb_output.create_sheet(title='PC Details')
for row in ws.rows:
rowInCorrectFormat = [cell.value for cell in row]
ws_output.append(rowInCorrectFormat)
for cell in row:
print(cell.value)
### THIS IS THE PART OF THE CODE WHICH DOES NOT WORK
ws_output.insert_cols(12)
ws_output['L5'] = 'OK or NOT GOOD?'
###
wb_output.save(r'path\test_Output_optimized.xlsx')
这正是我得到的错误:
ws_output.insert_cols(12)
AttributeError: 'WriteOnlyWorksheet' object has no attribute 'insert_cols'
这里的问题在于标志
write_only = True
。通过将此标志设置为true创建的工作簿与常规工作簿不同,如下所示。你知道吗插入列和插入行之类的函数也不适用于此类工作簿。你知道吗
可能的解决方案是不使用此标志或使用官方文档中建议的方法将数据添加到工作表中。你知道吗
对于使用工作簿,您可能会发现本文很有趣。https://medium.com/aubergine-solutions/working-with-excel-sheets-in-python-using-openpyxl-4f9fd32de87f
您可以在官方文档中阅读更多内容。https://openpyxl.readthedocs.io/en/stable/optimized.html
相关问题 更多 >
编程相关推荐