使用Python将数据写入Excel文件并将文本保存在精确的单元格中

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

我想问如何写入Excel文件字符串以查看输出Excel中的计算结果。

示例:

我想在单元格 L6 的 L 列中包含此字符串:=K6-L2+L5 并迭代整个表格,如下图所示:

这些单元格中的逻辑是这样的:

L6 = K6 - L2 + L5
M6 = L6 - M2 + M5
N6 = M6 - N2 + N5
O6 = N6 - O2 + O5

并且它应该位于具有类型 E 的每一列中。

我已经尝试过这样的操作,但我的代码没有在文件中创建任何更改。

import openpyxl
wb = openpyxl.load_workbook(filename='newfile.xlsx')
ws = wb.worksheets[0]
sb = [i for i in range(6,len(out),5)] #E cells
total = [i for i in range(2,len(out),5)]#A cells
dvl = [i for i in range(5,len(out),5)]#D cells

columns_names = ['K','L','M','N','O','P','R','S','T','U','V','W','X','Y','Z','AA','AB']
colNr = len(columns_names)

for i in sb:
    ws[f'K{i}'] = f'=G{i}'

for i in range(0,len(sb)):
    for cellnr in range(0,colNr):
        try:
            ws[f'{columns_names[cellnr+1]}{sb[i]}'] = f'={columns_names[cellnr]}{sb[i]} - {columns_names[cellnr+1]}{total[i]} + {columns_names[cellnr+1]}{dvl[i]}'
        except:
            pass
wb.save(filename='test.xlsx')

以下是数据:

Type        2023/07 2023/08 2023/09 2023/10 2023/11 2023/12 2024/01 2024/02 2024/03 2024/04 2024/05 2024/06 2024/07 2024/08 2024/09 2024/10 2024/11 2024/12
A       3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
B       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
C       3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
D       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
E       127 124 121 118 115 112 109 106 103 100 97  94  91  88  85  82  79  76
A       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
B       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
C       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
D       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
E       3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633    3633
A       5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
B       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
C       5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
D       0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
E       1637    1632    1627    1622    1617    1612    1607    1602    1597    1592    1587    1582    1577    1572    1567    1562    1557    1552
A       1   49  49  61  37  37  37  25  37  25  25  25  49  49  37  49  37  13
B       0   48  48  60  36  36  36  24  36  24  24  24  48  48  36  48  36  12
C       1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
D       10000   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
E       1023    974 925 864 827 790 753 728 691 666 641 616 567 518 481 432 395 382
python pandas excel string openpyxl
1个回答
1
投票

您可以使用 Openpyxl 翻译器跨行复制/更新公式。
插入初始公式

=K6-L2+L5

进入单元格 L6,然后在循环中使用它来更新其余部分;

示例代码

import openpyxl
from openpyxl.formula.translate import Translator
from openpyxl.utils.cell import get_column_letter as gcl
from openpyxl.utils.cell import column_index_from_string as cifs


wb = openpyxl.load_workbook(filename='foo.xlsx')
ws = wb['Sheet1']

orig_formula = "=K6-L2+L5"  # L is col 10
start_col = 'L'
row = 6

### Initial cell formula ('L6')
orig_cell = f'{start_col}{row}'

### Copy range starts at column L
rng_start = cifs(start_col)

### Use the Openpyxl Translator to update all the cells in row 6, 11, 16, 21 with the formula
### Loop inserts into each row per column
for col in range(rng_start, ws.max_column + 1):
    for cur_row in range(6, ws.max_row+1, 5):
        dst_cell = f"{gcl(col)}{cur_row}"
        ws[dst_cell] = Translator(orig_formula, origin=orig_cell).translate_formula(dst_cell)

wb.save('foo_out.xlsx')
© www.soinside.com 2019 - 2024. All rights reserved.