用另一个电子表格中的数据替换多个单元格中的模板

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

我正在寻找一种将数据从一个电子表格导入另一个电子表格的方法,但只能替换单元格内的占位符,而不是整个单元格。


上下文是我有一个目标电子表格,我在其中创建测试用例 - TargetTestplan.xlsx。这些测试用例被分解成单独的步骤,不超过 1 句话,但是我需要它们的 3 个版本,一个是英文字符串,一个是西班牙语字符串,一个是德语字符串。整个测试计划是用英文写的,但是需要翻译各种字符串(标题、按钮、链接名称等)。

目前我的工作流程是:

  1. 用需要翻译的字符串用英文写计划粗体
  2. 复制/粘贴英文计划并将所有粗体文本替换为西班牙文翻译
  3. 重复德语。

这有时每周会消耗 15 个小时,而且很容易出错。

TargetTestplan.xlsx 看起来像这样,只是规模更大,在 A 列和 B 列(测试步骤/预期结果)的多个单元格中可能有多个相同翻译的实例。这个想法是 fstring 中的数字与翻译所在的行相关:

我有一个由公司提供给我的单独电子表格,它有 3 列(每种语言 1 列)- 每行都是 3 列中彼此的翻译:

我尝试了多种解决方案,同时使用 pandas 和 openpyxl,我认为这已经很接近了,但我无法让它工作 - 我得到的退出代码为 0,创建了 TargetTestplan_updated.xlsx,但 fstrings 是未更换:

# Import the openpyxl module
import openpyxl

# Load the translation sheet
translation_wb = openpyxl.load_workbook('TranslationTemplate.xlsx')
translation_ws = translation_wb.active

# Create dictionaries for each language
en_dict = {}
es_dict = {}
de_dict = {}

# Iterate over each row in the translation sheet and add the values to the dictionaries
for row in translation_ws.iter_rows(min_row=1, values_only=True):
    en_dict[row[0]] = row[0]
    es_dict[row[0]] = row[1]
    de_dict[row[0]] = row[2]

# Load the target sheet
testplan_wb = openpyxl.load_workbook('TargetTestplan.xlsx')
testplan_ws = testplan_wb.active

# Iterate over each cell in the target sheet
for row in testplan_ws.iter_rows(min_row=1, values_only=False):
    for cell in row:
        # Get the row and column numbers for the current cell
        row_number, column_number = cell.row, cell.column

        # Get the cell value and replace any fstrings with the corresponding translation
        cell_value = str(cell.value)
        cell_value = cell_value.replace(f'f{{en{row_number - 1}}}', en_dict.get(row_number - 1, ''))
        cell_value = cell_value.replace(f'f{{es{row_number - 1}}}', es_dict.get(row_number - 1, ''))
        cell_value = cell_value.replace(f'f{{de{row_number - 1}}}', de_dict.get(row_number - 1, ''))

        # Update the cell value
        testplan_ws.cell(row=row_number, column=column_number, value=cell_value)

# Save the updated target sheet
testplan_wb.save('TargetTestplan_updated.xlsx')

我是在正确的轨道上还是在有更简单的解决方案时这是否过于复杂?

python excel validation translation
© www.soinside.com 2019 - 2024. All rights reserved.