为什么我不能将 Excel 值放在标题下?

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

[在此输入图片描述][在此输入图片描述](https://i.stack.imgur.com/KcItK.png)(https://i.stack.imgur.com/tdIAc.png)

我有一个像第一张图片一样的Excel表格。我想像第二张图片一样编辑。我使用 openpyxl,我可以放置标题(1,2,3,4..),但我无法放置值


from  openpyxl import *
mybook = load_workbook("newtry.xlsx")
myvalue= []
sheet=mybook.active
for row in sheet.iter_rows(min_row=1, min_col=1, max_row=5, max_col=6):
        for cell in row:
            myvalue.append(cell.value)


x=1
for i in myvalue:
    x=x+1
    if i == 1:
        for row, entry in enumerate('i', start=1):
            sheet.cell(row=x, column=1, value=1)
    book.save("nwetry2.xlsx")

尝试这样

python excel database openpyxl
1个回答
0
投票

有点复杂,但可以用 Pandas 来完成

使用 Pandas 将带有值的单元格向左移动。
在移位之前将每个组的 Header 转储到 List 中,以便在移位后插入数据。

import pandas as pd


def shift_cols_left(x, ncd):
    original_columns = x.index.tolist()

    ### Drop NaN cells
    shifted = x.dropna()

    cur_row = x.name  # Current Row in the DataFrame being compressed
    ### Create a dictionary of the Headers to be added as new columns
    ### This will lists of the original Column Headers for the data. To be inserted into the compressed DataFrame
    for col_count, col in enumerate(shifted):
        section_header = shifted.index[shifted == col].values[0]
        if 'Unnamed' not in str(section_header):  # Use actual Header names only
            cur_col = f"col{col_count}"
            if cur_col in ncd:
                ncd[f"{cur_col}"] += [section_header]
            else:
                if cur_row > 0:  # Pad the list if needed
                    for x in range(cur_row):
                        if cur_col in ncd:
                            ncd[f"{cur_col}"] += ['']
                        else:
                            ncd[f"{cur_col}"] = ['']
                    ncd[f"{cur_col}"] += [section_header]
                else:
                    ncd[f"{cur_col}"] = [section_header]

    ### Shift columns with values to the left removing gaps and update column headers
    shifted.index = [original_columns[n] for n in range(shifted.count())]

    return shifted


filepath = 'newtry.xlsx'
sheet = 'Sheet1'

new_col_dict = {}

### Read the original Data from Excel
df = pd.read_excel(filepath, sheet_name=sheet)
print(f"Original DataFrame:\n{df}\n----------------------------------\n")

### Remove empty cells and shift data to the left
df1 = df.apply(shift_cols_left, args=(new_col_dict,), axis=1)
print(f"Left shifted DataFrame\n{df1}\n----------------------------------\n")

### Insert the Header detail into the DataFrame at first row then each 3rd row as necessary
loc = 0
for k, v in new_col_dict.items():
    df1.insert(loc=loc, column=k, value=pd.Series(v))
    loc += 3

### Final DataFrame with shifted data and Header Columns inserted
### Not bothering with renaming Headers as these will be dropped when writing to Excel
print(f"Left shifted DataFrame with header columns included:\n{df1}\n----------------------------------\n")

### Write the resultant DataFrame to Excel
### Drop Index and Header
with pd.ExcelWriter('newtry2.xlsx') as writer: 
    df1.to_excel(writer, sheet_name='Sheet1', index=False, header=False)

输入表
该表是问题中显示的表的副本。我假设第 1 行的标题是合并单元格。
IE。 A1 & B1、C1 & D1、E1 & F1 以及 G1 & H1 单元格单独合并。

输出表

代码示例将创建一个 new 'newtry2.xlsx'(覆盖同一目录中具有该名称的任何现有文件)。
如果需要,可以将写入器更改为写入现有文件而不覆盖。还可以将其写入任意位置的现有工作表或新工作表。

如问题输出图像所示,不包含标题。
标题可以在 DataFrame 中更改/更新,并包含在写入 Excel 中,或者
使用 ExcelWriter 使用的 ExcelWriter 引擎写入 DataFrame 后可以插入标头。

© www.soinside.com 2019 - 2024. All rights reserved.