Openpyxl 复制具有命名范围的工作表

问题描述 投票:0回答:1

我可以复制我需要的工作表,但它不会复制命名范围,因此在单元格中我出现名称错误,因为公式正在使用该名称进行微积分。当然,名称需要与地址匹配。 正确的做法是什么?

这是我的代码。

def write_to_template_sheet(input_filename, value_to_write):
    try:
        # Cargar el archivo Excel
        wb = openpyxl.load_workbook(input_filename)

        # Crear una copia de la hoja "Template"
        template_sheet = wb["Template"]
        new_sheet = wb.copy_worksheet(template_sheet)

        # Contar el número de hojas que comienzan con "P"
        hojas_con_p = [nombre for nombre in wb.sheetnames if nombre.startswith('P')]
        numero_hojas_con_p = len(hojas_con_p)

        # Renombrar la nueva hoja con "P" seguido del número de hojas con "P"
        new_sheet.title = f"P{numero_hojas_con_p +1}"

        # Copiar datos y formatos
        for row in template_sheet.iter_rows(min_row=1, max_row=template_sheet.max_row, min_col=1, max_col=template_sheet.max_column):
            
            for cell in row:
                    new_cell = new_sheet[cell.coordinate]
                    new_cell.value = cell.value
                   
        # Escribir el valor en la fila 31, columna 2 de la nueva hoja
        new_sheet.cell(row=31, column=2, value=value_to_write)

        # Guardar los cambios en el archivo
        wb.save(input_filename)

        st.success('Saved successfully')
        except Exception as e:
        st.error(f'Error: {str(e)}')


st.title("Create a project")


value_to_write = st.text_input("Name of the proyect", "",key="Project_Name")


if st.button("Save"):
    input_filename = "wbtool.xlsx"  # Nombre del archivo original
    write_to_template_sheet(input_filename, value_to_write)

我已经尝试过了

import streamlit as st
import openpyxl
from openpyxl import Workbook

wb = openpyxl.load_workbook('wbtool.xlsx')
sh = wb\['Template'\]
allnames=list(sh.defined_names.names)

st.write(allnames)

但我不知道如何使用它来复制姓名和地址。

python excel openpyxl streamlit
1个回答
0
投票

您已使用 Openpyxl 'copy_worsheet' 函数制作了工作表的副本

new_sheet = wb.copy_worksheet(template_sheet)

因此后面的命令循环遍历单元格并复制 # Copar datos y formatos 处的值是多余的。本节不会执行任何 copy_worksheet 尚未执行的操作。

对于定义的名称;创建新工作表后,您可以阅读并重新创建。在此示例中,我假设模板定义的名称的范围仅限于工作表,以便新工作表中使用的相同名称是分开的。
因此,只需从“template_sheet”中读取定义的名称,然后为每个定义的名称重新创建并添加新工作表即可。
如果需要的话可以修改以适应

import openpyxl
import streamlit as st


def write_to_template_sheet(input_filename, value_to_write):
    try:
        # Cargar el archivo Excel
        wb = openpyxl.load_workbook(input_filename)

        # Crear una copia de la hoja "Template"
        template_sheet = wb["Template"]
        ### Copies the sheet here all styling and values (but not defined names)
        new_sheet = wb.copy_worksheet(template_sheet)

        # Contar el número de hojas que comienzan con "P"
        hojas_con_p = [nombre for nombre in wb.sheetnames if nombre.startswith('P')]
        numero_hojas_con_p = len(hojas_con_p)

        # Renombrar la nueva hoja con "P" seguido del número de hojas con "P"
        new_sheet.title = f"P{numero_hojas_con_p + 1}"

        ### Copy the defined names 
        for name, items in template_sheet.defined_names.items():
            ### change the sheet name in the coordinates
            coords = items.attr_text.replace(template_sheet.title, new_sheet.title)
            ### Create the new defined name for the new sheet using the same defined name e.g. data1 
            ### and the coords from the template sheet with sheet name changed
            new_dn = openpyxl.workbook.defined_name.DefinedName(
                name, attr_text=coords)
            ### Add the defined name scoped to the sheet
            new_sheet.defined_names.add(new_dn)

        ### This section is redundant cell values are already copied
        # Copiar datos y formatos
        # for row in template_sheet.iter_rows(min_row=1, max_row=template_sheet.max_row, min_col=1,
        #                                     max_col=template_sheet.max_column):
        #
        #     for cell in row:
        #         new_cell = new_sheet[cell.coordinate]
        #         new_cell.value = cell.value

        # Escribir el valor en la fila 31, columna 2 de la nueva hoja
        new_sheet.cell(row=31, column=2, value=value_to_write)

        # Guardar los cambios en el archivo
        wb.save(input_filename)

        st.success('Saved successfully')
    except Exception as e:
        st.error(f'Error: {str(e)}')
© www.soinside.com 2019 - 2024. All rights reserved.