提取excel表头

317 篇文章 3 订阅
import xlrd
from control_excel import convert_to_letter

xlsx_path = r'D:\code\yangming\file\output=CZ16 20220718X.xlsx'
data = xlrd.open_workbook(xlsx_path)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
print(nrows, ncols)
l = []
for i in range(21):
    name = table.cell(0, i).value
    if i == 20:
        name = 'Unit Price'
    letter = convert_to_letter(i)
    print(i, letter, name)
    item_dict = {
        letter: name
    }
    l.append(item_dict)

print(l)

原始表格

在这里插入图片描述

效果

[{'A': 'TO#'}, {'B': 'Preparation Date'}, {'C': 'Doc No'}, {'D': 'PN'}, {'E': 'Vendor PN'}, {'F': 'Cust'}, {'G': 'RCV Doc NO'}, {'H': 'Exp.Qty'}, {'I': 'Shp.Qty'}, {'J': 'Vendor'}, {'K': 'Sup.Vend.'}, {'L': 'PO'}, {'M': 'REF No'}, {'N': 'Move Type'}, {'O': 'CustRec Date'}, {'P': 'Purchase Date'}, {'Q': 'Conf.Qty'}, {'R': 'Lot#'}, {'S': 'Billing PO'}, {'T': 'Billing PO Item'}, {'U': 'Unit Price'}]

写表头

 workbook = xlsxwriter.Workbook(file_path)
    worksheet = workbook.add_worksheet()

    deep_green = workbook.add_format({'bg_color': '#99cc00'})
    light_green = workbook.add_format({'bg_color': '#d8e4bc'})

    # Widen the first column to make the text clearer.
    worksheet.set_column('A:D', 18)
    worksheet.set_column('G:G', 20)
    worksheet.set_column('K:K', 20)
    worksheet.set_column('M:M', 20)
    worksheet.set_column('O:O', 20)
    l = [{'A': 'S'}, {'B': '收貨 Date'}, {'C': 'Doc No'}, {'D': 'PN'}, {'E': 'Vendor PN'}, {'F': 'Cust'}, {'G': 'RCV Doc NO'}, {'H': 'Rec.Qty'}, {'I': 'Vendor'}, {'J': 'Sup.Vend.'}, {'K': '櫃號'}, {'L': 'Move Type'}, {'M': '轉倉DN'}, {'N': '下載DATE'}, {'O': 'CountryOfOr.'}, {'P': 'DN line'}, {'Q': ''}, {'R': ''}, {'S': ''}]
    for i in l:
        for k, v in i.items():
            letter = f'{k}1'
            worksheet.write(letter, v)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值