如何使用`openpyxl`库在Excel中的合并单元格中写入?

Ale*_*der 6 python excel for-loop pandas openpyxl

我正在使用openpyxl库在单独的单元格中写入现有的 Excel 文件。

如何在 Excel 合并单元格中写入一些文本?

错误 AttributeError: 'MergedCell' object attribute 'value' is read-only

合并单元格时:

代码:

        wb = openpyxl.load_workbook(filename=src)
        for row in df_short.itertuples():
            ws = wb[row.sheet]
            try:
                cell = 'N'+str(row.id)
                ws[cell] = '=HYPERLINK("%s","#%s")' % (row.txt_path, row.txt)

Run Code Online (Sandbox Code Playgroud)

小智 -2

我在 python3.5 中遇到了这个确切的错误和 conda 环境的解决方案是

conda install -c conda-forge openpyxl=2.5.14 -y
Run Code Online (Sandbox Code Playgroud)

对于 pip3 环境也是如此:

pip3 install openpyxl==2.5.14 -y
Run Code Online (Sandbox Code Playgroud)