Python excel转pdf|金额大写工具类

# 一些工具函数
class Util:

    # 基于excel生成pdf
    @staticmethod
    def excel_to_pdf(base_path, target_excel_name, target_pdf_name, center_code_gi_map):
        pass
        # # Open Microsoft Excel
        # excel = client.Dispatch("Excel.Application")
        # # Read Excel File
        # sheets = excel.Workbooks.Open(base_path + "/" + target_excel_name)
        # for sheet in sheets.Worksheets:
        #     pdf_name = target_pdf_name.replace('XXX', sheet.name).replace('GI', center_code_gi_map.get(sheet.name))
        #     print("开始导出pdf:"+pdf_name)
        #     # Convert into PDF File
        #     folder = os.path.exists(base_path + "/" + center_code_gi_map.get(sheet.name))
        #     # 没有的话先创建
        #     if not folder:
        #         os.makedirs(base_path + "/" + center_code_gi_map.get(sheet.name))
        #     sheet.ExportAsFixedFormat(0, base_path + "/" + center_code_gi_map.get(sheet.name) + "/" + pdf_name + ".pdf")

    # 将金额大写
    @staticmethod
    def up_money(digital):
        str_digital = str(digital)
        # 去掉数字里面的逗号
        str_digital = str_digital.replace(",", "")
        chinese = {'1': '', '2': '', '3': '', '4': '', '5': '', '6': '', '7': '', '8': '', '9': '',
                   '0': ''}
        chinese2 = ['', '', '', '', '', '', '']
        jiao = ''
        bs = str_digital.split('.')
        yuan = bs[0]
        if len(bs) > 1:
            jiao = bs[1]
        r_yuan = [i for i in reversed(yuan)]
        count = 0
        for i in range(len(yuan)):
            if i == 0:
                r_yuan[i] += ''
                continue
            r_yuan[i] += chinese2[count]
            count += 1
            if count == 4:
                count = 0
                chinese2[3] = '亿'

        s_jiao = [i for i in jiao][:3]  # 去掉小于厘之后的

        j_count = -1
        for i in range(len(s_jiao)):
            s_jiao[i] += chinese2[j_count]
            j_count -= 1
        last = [i for i in reversed(r_yuan)] + s_jiao

        last_str = ''.join(last)
        for i in range(len(last_str)):
            digital = last_str[i]
            if digital in chinese:
                last_str = last_str.replace(digital, chinese[digital])
        return last_str

    # 格式化金额 - 时返回0 ,0.0时返回0,增加千分位和保留两位小数
    @staticmethod
    def format_money(money):
        if type(money) == str and '-' == money:
            return '0'
        if type(money) == float and int(money) == 0:
            return '0'
        if type(money) == float or type(money) == int:
            money = round(money, 2)
            return format(money, ',')
        return money

 

posted @ 2023-10-31 14:53  沙琪玛  阅读(44)  评论(0编辑  收藏  举报