python中关于发票金额的中文转换和连接打印机的api

需求为根据发票的金额,计算出对应的中文,其实就是一个对于list的应用:

写了一个一键打印收款收据的功能,其中感觉比较能复用的就是金额的中文转。

后台收的数据是decimal数据类型的金额自己写了一个脚本,能够直接转换,有相同需求的小伙伴可以复用,代码如下:

excel_unit = ['分', '角', '元', '拾', '佰', '仟', '万', '拾', '佰', '仟']
math_dict = {
    '1': '壹',
    '2': '贰',
    '3': '叁',
    '4': '肆',
    '5': '伍',
    '6': '陆',
    '7': '柒',
    '8': '捌',
    '9': '玖',
}



def to_transfer(amount:decimal):
    reversed_list = reversed_amount(str(amount))
    amount_c = to_transfer_c(reversed_list)
    return amount




def to_transfer_c(reversed_list: list):
    boolean_zero = False
    amount_c = ''
    index_excel = 0
    for x in reversed_list:
        if index_excel == 6 and x == '0':
            amount_c = '万' + amount_c
        if x == '0':
            if boolean_zero:
                amount_c = '零' + amount_c
                boolean_zero = False
        else:
            amount_c = math_dict.get(x) + excel_unit[index_excel] + amount_c
            boolean_zero = True
        index_excel += 1
    chars = ['分', '角']
    if not contains_chars(amount_c, chars):
        amount_c = amount_c + '元整'
    return amount_c


# 反转字符串 成为list
def reversed_amount(amount: str):
    str_amount = amount
    reversed_list = list(str_amount[::-1])
    return reversed_list

实现效果:

补充:python连接打印机比较简单的一个api,相关代码。

def to_print_reception(file_path: str):
    app = xw.App(visible=False, add_book=False)
    workbook = app.books.open(file_path)
    worksheet = workbook.sheets[0]
    worksheet.api.PageSetup.Zoom = 130  # 按工作表原始大小的140%进行打印
    worksheet.api.PageSetup.CenterHorizontally = True  # 调整工作表在纸张上的水平位置
    worksheet.api.PageSetup.CenterVertically = True  # 调整工作表在纸张上的垂直位置
    # worksheet.api.PrintOut(Copies=1, ActivePrinter='HP LaserJet MFP M232-M237 PCLm-S(网络)', Collate=True)
    worksheet.api.PrintOut(Copies=1, ActivePrinter='Microsoft Print to PDF', Collate=True)
    workbook.close()
    app.quit()

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值