Python Openpyxl - 在 write_only 电子表格中添加列
Python Openpyxl - Add column in write_only spreadsheet
我正在使用 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 创建的工作簿与常规工作簿不同,如下所示。
insert_cols 和 insert_rows 等函数也不适用于此类工作簿。
可能的解决方案可能是不使用此标志或使用官方文档中建议的方式将数据添加到 sheet。
对于使用工作簿,您可能还会发现这篇文章很有趣。 https://medium.com/aubergine-solutions/working-with-excel-sheets-in-python-using-openpyxl-4f9fd32de87f
您可以在官方文档中阅读更多内容。 https://openpyxl.readthedocs.io/en/stable/optimized.html
我正在使用 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 创建的工作簿与常规工作簿不同,如下所示。
insert_cols 和 insert_rows 等函数也不适用于此类工作簿。
可能的解决方案可能是不使用此标志或使用官方文档中建议的方式将数据添加到 sheet。
对于使用工作簿,您可能还会发现这篇文章很有趣。 https://medium.com/aubergine-solutions/working-with-excel-sheets-in-python-using-openpyxl-4f9fd32de87f
您可以在官方文档中阅读更多内容。 https://openpyxl.readthedocs.io/en/stable/optimized.html