convert.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import zipfile
  2. import openpyxl
  3. import xlrd
  4. import csv
  5. import plac
  6. from pathlib import Path
  7. def convert_folder(folder=None):
  8. if folder is None:
  9. folder = str(Path(__file__).parent)
  10. if Path(folder).is_file():
  11. xls_file = Path(folder)
  12. if xls_file.name[-4:] == ".xls":
  13. convert_old(xls_file)
  14. elif xls_file.name[-5:] == ".xlsx":
  15. convert(xls_file)
  16. return
  17. for xls_file in Path(folder).absolute().glob("*.xls"):
  18. convert_old(xls_file)
  19. for xls_file in Path(folder).absolute().glob("*.xlsx"):
  20. convert(xls_file)
  21. def localize_floats(el):
  22. return str(el).replace(".", ",") if isinstance(el, float) else el
  23. def convert_old(xls_file: Path):
  24. print("\n" + str(xls_file))
  25. try:
  26. workbook = xlrd.open_workbook(xls_file, ignore_workbook_corruption=True)
  27. except xlrd.XLRDError as e:
  28. print(e)
  29. return
  30. # first sheet as master without suffix
  31. csv_file = f"{xls_file.parent}/{xls_file.stem}.csv"
  32. print("-> " + csv_file)
  33. ws = workbook.sheet_by_index(0)
  34. with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
  35. wr = csv.writer(fwh, delimiter=";")
  36. for rownum in range(ws.nrows):
  37. wr.writerow([localize_floats(el) for el in ws.row_values(rownum)])
  38. for sheet in workbook.sheet_names():
  39. csv_file = f"{xls_file.parent}/{xls_file.stem}_{sheet}.csv"
  40. print("-> " + csv_file)
  41. ws = workbook.sheet_by_name(sheet)
  42. with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
  43. wr = csv.writer(fwh, delimiter=";")
  44. for rownum in range(ws.nrows):
  45. wr.writerow([localize_floats(el) for el in ws.row_values(rownum)])
  46. def convert(xlsx_file: Path):
  47. print("\n" + str(xlsx_file))
  48. try:
  49. workbook = openpyxl.load_workbook(xlsx_file, data_only=True)
  50. except zipfile.BadZipFile as e:
  51. print(e)
  52. return
  53. # first sheet as master without suffix
  54. csv_file = f"{xlsx_file.parent}/{xlsx_file.stem}.csv"
  55. print("-> " + csv_file)
  56. sheet = workbook.worksheets[0]
  57. with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
  58. wr = csv.writer(fwh, delimiter=";")
  59. for row in sheet.iter_rows():
  60. wr.writerow([localize_floats(cell.value) for cell in row])
  61. for sheet in workbook:
  62. csv_file = f"{xlsx_file.parent}/{xlsx_file.stem}_{sheet.title}.csv"
  63. print("-> " + csv_file)
  64. with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
  65. wr = csv.writer(fwh, delimiter=";")
  66. for row in sheet.iter_rows():
  67. wr.writerow([localize_floats(cell.value) for cell in row])
  68. if __name__ == "__main__":
  69. plac.call(convert_folder)