txt处理成excel

样式:

添加筛选: https://openpyxl-chinese-docs.readthedocs.io/zh_CN/latest/filters.html

writer = pd.ExcelWriter(file_s)
writer.sheets[sheet_n].freeze_panes = 'A2'  # 冻结表头
writer.sheets[sheet_n].auto_filter.ref = "A1:Z2" # 添加筛选

  

冻结

#https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html?highlight=freeze_panes

this_df.to_excel(writer, sheet_n, index=False, freeze_panes=(1, 1))

  

识别文件编码:
import chardet
 
# 获取文件编码类型
def get_encoding(file):
    # 二进制方式读取,获取字节数据,检测类型
    with open(file, 'rb') as f:
        return chardet.detect(f.read())['encoding']
 
 
file_name = 'my.ini'
encoding = get_encoding(file_name)
this_df = pd.read_csv(file, delimiter="\t", encoding=encoding )
this_df.to_excel(writer, sheet_n, index=False)

  

 
posted @ 2022-05-26 14:07  小毛编  阅读(280)  评论(0编辑  收藏  举报