Я пытаюсь скопировать лист из одной книги в другую и вставить все содержимое вместе с исходным форматированием. В моем исходном листе есть формулы, и мне нужно копировать только значения с исходным форматированием в другую книгу.
Мне удалось добиться исходного форматирования с помощью openpyxl и при попытке скопировать значение с параметром Data_only = True Я не вижу никаких скопированных значений. Как мне копировать значения без формул?
ниже приведен код
# Load to New workbook
target_file = tgtfilepath+'\Finalextracts'+'\/'+'WB_TGT'+filedate+'.xlsx'
wb_target = openpyxl.load_workbook(target_file)
# Select the sheet where you want to paste data
target_sheet = wb_target["sheet_1"]
# Load the source workbook (this is where we get the data from)
source_file = tgtfilepath+'\Temp'+'\/'+'WB_src'+filedate+'_Temp.xlsx'
wb_source = openpyxl.load_workbook(source_file, data_only=True) # data_only=True to get values, no formulas
source_sheet = wb_source.active # Assuming data is in the active sheet of the source workbook
# Define the range to copy (example: A1 to D10)
source_range = source_sheet['A1:Z49'] # Adjust the range as needed
target_range_start_row = 1 # Adjust where you want to start pasting the data in target sheet
# Loop through the source range and copy only values to the target sheet
for row_index, row in enumerate(source_range, start=target_range_start_row):
for col_index, cell in enumerate(row, start=1):
target_cell = target_sheet.cell(row=row_index, column=col_index)
# Copy only the value (no formula)
target_cell.value = cell.value
target_cell.font = copy(cell.font)
target_cell.alignment = copy(cell.alignment)
target_cell.border = copy(cell.border)
target_cell.fill = copy(cell.fill)
# Save the modified workbook with values copied into the existing formatted sheet
wb_target.save(tgtfilepath+'\Finalextracts'+'\/'+'sheet_1'+filedate+'.xlsx')
Подробнее здесь: https://stackoverflow.com/questions/792 ... g-openpyxl
Копирование и вставка значений Excel с исходным форматированием с использованием OpenPyxl ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Python Openpyxl: копирование и вставка из одной книги Excel в другую – Содержание
Anonymous » » в форуме Python - 0 Ответы
- 46 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Openpyxl не изменяет обновленные функции Excel в сгенерированном Excel
Anonymous » » в форуме Python - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-