Python扫描枪读取发票数据导入Excel

财务需要一个扫描枪扫描发票文件,并将主要信息录入Excel 的功能。

文件中sheet表的列名称,依次为:发票编号、发票编码、日期、金额、工号、扫描日期。
扫描的时候,Excel 文件需要关闭,否则会报错。

import openpyxl
import datetime


def write_line_excel(text):
    if text == '':
        return
    work_book = openpyxl.load_workbook('fapiaosaomiao.xlsx')
    sheet = work_book.active
    max_rows = sheet.max_row

    # 获取发票编码 一列
    fapiaoList = [cell.value for cell in sheet['B']]
    # 解析文本
    textList = text.split(',')
    # 判断发票是否重复,如果重复,就直接打印发票重复,实际业务需要
    if textList[3] in fapiaoList:
        print("发票编码重复:" + textList[3])
        return
        
    # 这里的判断是因为有2种类型的发票文件
    # 判断第3个元素内容,如果是空,那么是新版,如果是数字,那么是旧发票
    if textList[2] != '':
        # 发票编号
        sheet.cell(max_rows + 1, 1, textList[2])

    # 发票编码
    sheet.cell(max_rows + 1, 2, textList[3])
    # 日期
    # 这里只是修改日期的显示格式,实际业务需要
    date_str = textList[5][:4] + '-' + textList[5][4:6] + '-' + textList[5][-2:]
    sheet.cell(max_rows + 1, 3, date_str)
    # 金额
    sheet.cell(max_rows + 1, 4, textList[4])
    # 工号,填空即可,实际业务需要
    sheet.cell(max_rows + 1, 5, 0)
    # 扫描日期
    # 获取当前日期和时间
    current_datetime = datetime.datetime.now()
    # 格式化当前日期和时间
    formatted_datetime = current_datetime.strftime('%Y/%m/%d %H:%M:%S')
    sheet.cell(max_rows + 1, 6, formatted_datetime)
    work_book.save("fapiaosaomiao.xlsx")


def main():
    while True:
        scanned_data = input("扫描内容或指令:")
        if scanned_data == 'q':
            exit()

        write_line_excel(scanned_data)


if __name__ == "__main__":
    main()

来源链接:https://www.cnblogs.com/studyming/p/18668489

请登录后发表评论

    没有回复内容