需求为根据发票的金额,计算出对应的中文,其实就是一个对于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()
5914

被折叠的 条评论
为什么被折叠?



