赞
踩
需求是要追加sheet写入原文件,所以engine还是得用openpyxl
在写入文件前,用openpyxl的ILLEGAL_CHARACTERS_RE清除下不支持的字符
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
import pandas as pd
df = pd.read_excel(excel_filepath, sheet_name=sheet_name)
# do something
# 写入原文件前清除openpyxl不支持的字符
for col in df.columns:
df[col] = df[col].apply(lambda x: ILLEGAL_CHARACTERS_RE.sub(r'', str(x) if not pd.isna(x) else ''))
with pd.ExcelWriter(excel_filepath, engine='openpyxl', mode='a') as writer:
df.to_excel(writer, sheet_name='new sheet', index=False)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。