common.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pdfkit
  2. import config
  3. import sys
  4. import os
  5. han_list = ["零" , "一" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九"]
  6. unit_list = ["","","十" , "百" , "千","万","十万","百万","千万","亿"]
  7. def four_to_han(num_str):
  8. result = ""
  9. num_len = len(num_str)
  10. for i in range(num_len):
  11. num = int(num_str[i])
  12. if i!=num_len-1:
  13. if num!=0:
  14. result=result+han_list[num]+unit_list[num_len-i]
  15. else:
  16. if result[-1]=='零':
  17. continue
  18. else:
  19. result=result+'零'
  20. else:
  21. if num!=0:
  22. result += han_list[num]
  23. return result
  24. def convert_to_pdf(sourceHtml,outpdffile):
  25. path=resource_path(os.path.join("wkhtmltopdf","bin/wkhtmltopdf.exe"))
  26. configuration = pdfkit.configuration(wkhtmltopdf=path)
  27. options = {
  28. "enable-local-file-access": None,
  29. }
  30. try:
  31. pdfkit.from_file(sourceHtml, outpdffile, configuration=configuration, options=options)
  32. except Exception as e:
  33. {
  34. # print(str(e))
  35. }
  36. #生成资源文件目录访问路径
  37. def resource_path(relative_path):
  38. if getattr(sys, 'frozen', False): #是否Bundle Resource
  39. base_path = sys._MEIPASS
  40. else:
  41. base_path = os.path.abspath(".")
  42. return os.path.join(base_path, relative_path)