Browse Source

Excel auf xlwings umgestellt und bei König getestet

gc-server3 11 months ago
parent
commit
1a3421bd77
5 changed files with 78 additions and 33 deletions
  1. BIN
      dist/gctools.exe
  2. 31 24
      excel/convert.py
  3. 43 7
      excel/refresh.py
  4. 3 1
      requirements.txt
  5. 1 1
      xls.py

BIN
dist/gctools.exe


+ 31 - 24
excel/convert.py

@@ -9,18 +9,25 @@ from pathlib import Path
 def convert_folder(folder=None):
     if folder is None:
         folder = str(Path(__file__).parent)
-    for xls_file in Path(folder).absolute().glob('*.xls'):
+    if Path(folder).is_file():
+        xls_file = Path(folder)
+        if xls_file.name[-4:] == ".xls":
+            convert_old(xls_file)
+        elif xls_file.name[-5:] == ".xlsx":
+            convert(xls_file)
+        return
+    for xls_file in Path(folder).absolute().glob("*.xls"):
         convert_old(xls_file)
-    for xls_file in Path(folder).absolute().glob('*.xlsx'):
+    for xls_file in Path(folder).absolute().glob("*.xlsx"):
         convert(xls_file)
 
 
 def localize_floats(el):
-    return str(el).replace('.', ',') if isinstance(el, float) else el
+    return str(el).replace(".", ",") if isinstance(el, float) else el
 
 
 def convert_old(xls_file: Path):
-    print('\n' + str(xls_file))
+    print("\n" + str(xls_file))
     try:
         workbook = xlrd.open_workbook(xls_file, ignore_workbook_corruption=True)
     except xlrd.XLRDError as e:
@@ -28,49 +35,49 @@ def convert_old(xls_file: Path):
         return
 
     # first sheet as master without suffix
-    csv_file = f'{xls_file.parent}/{xls_file.stem}.csv'
-    print('-> ' + csv_file)
+    csv_file = f"{xls_file.parent}/{xls_file.stem}.csv"
+    print("-> " + csv_file)
     ws = workbook.sheet_by_index(0)
-    with open(csv_file, 'w', encoding='latin-1') as fwh:
-        wr = csv.writer(fwh, delimiter=';')
+    with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
+        wr = csv.writer(fwh, delimiter=";")
         for rownum in range(ws.nrows):
             wr.writerow([localize_floats(el) for el in ws.row_values(rownum)])
 
     for sheet in workbook.sheet_names():
-        csv_file = f'{xls_file.parent}/{xls_file.stem}_{sheet}.csv'
-        print('-> ' + csv_file)
+        csv_file = f"{xls_file.parent}/{xls_file.stem}_{sheet}.csv"
+        print("-> " + csv_file)
         ws = workbook.sheet_by_name(sheet)
-        with open(csv_file, 'w', encoding='latin-1') as fwh:
-            wr = csv.writer(fwh, delimiter=';')
+        with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
+            wr = csv.writer(fwh, delimiter=";")
             for rownum in range(ws.nrows):
                 wr.writerow([localize_floats(el) for el in ws.row_values(rownum)])
 
 
-def convert(xls_file: Path):
-    print('\n' + str(xls_file))
+def convert(xlsx_file: Path):
+    print("\n" + str(xlsx_file))
     try:
-        workbook = openpyxl.load_workbook(xls_file)
+        workbook = openpyxl.load_workbook(xlsx_file, data_only=True)
     except zipfile.BadZipFile as e:
         print(e)
         return
 
     # first sheet as master without suffix
-    csv_file = f'{xls_file.parent}/{xls_file.stem}.csv'
-    print('-> ' + csv_file)
+    csv_file = f"{xlsx_file.parent}/{xlsx_file.stem}.csv"
+    print("-> " + csv_file)
     sheet = workbook.worksheets[0]
-    with open(csv_file, 'w', encoding='latin-1') as fwh:
-        wr = csv.writer(fwh, delimiter=';')
+    with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
+        wr = csv.writer(fwh, delimiter=";")
         for row in sheet.iter_rows():
             wr.writerow([localize_floats(cell.value) for cell in row])
 
     for sheet in workbook:
-        csv_file = f'{xls_file.parent}/{xls_file.stem}_{sheet.title}.csv'
-        print('-> ' + csv_file)
-        with open(csv_file, 'w', encoding='latin-1') as fwh:
-            wr = csv.writer(fwh, delimiter=';')
+        csv_file = f"{xlsx_file.parent}/{xlsx_file.stem}_{sheet.title}.csv"
+        print("-> " + csv_file)
+        with open(csv_file, "w", encoding="latin-1", errors="replace", newline="") as fwh:
+            wr = csv.writer(fwh, delimiter=";")
             for row in sheet.iter_rows():
                 wr.writerow([localize_floats(cell.value) for cell in row])
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     plac.call(convert_folder)

+ 43 - 7
excel/refresh.py

@@ -1,17 +1,28 @@
+import zipfile
+import xlwings
 import win32com.client
 from pathlib import Path
 
 
 def refresh(filename_str: str):
     filename = Path(filename_str)
+    if not filename.exists():
+        print("!! Error: File does not exist !!")
+        return
+    if filename.name.endswith(".xls"):
+        return refresh_old(filename)
+    return refresh_new(filename)
+
+
+def refresh_old(xls_file: Path):
+    print(f"Refresh '{xls_file.resolve()}'")
+
     excel = win32com.client.Dispatch("Excel.Application")
     excel.DisplayAlerts = False
-
-    print(f"Refresh '{filename.resolve()}'")
-    wb = excel.Workbooks.Open(filename.resolve())
+    workbook = excel.Workbooks.Open(xls_file.resolve())
 
     try:
-        for source in wb.LinkSources(1):
+        for source in workbook.LinkSources(1):
             print(" --> " + source)
             if Path(source).exists():
                 excel.Workbooks.Open(source)
@@ -20,7 +31,32 @@ def refresh(filename_str: str):
     except TypeError:
         print(" --> " + "no LinkSources")
 
-    wb.RefreshAll()
-    wb.Save()
-    wb.Close(True)
+    workbook.RefreshAll()
+    workbook.Save()
+    workbook.Close(True)
     excel.Application.Quit()
+
+
+def refresh_new(xlsx_file: Path):
+    print(f"Refresh '{xlsx_file.resolve()}'")
+    try:
+        excel = xlwings.App(visible=False)
+        workbook = xlwings.Book(xlsx_file, update_links=False)
+    except zipfile.BadZipFile as e:
+        print(e)
+        return
+
+    try:
+        for source in workbook.api.LinkSources():
+            print(" --> " + source)
+            if Path(source).exists():
+                xlwings.Book(source, update_links=False, read_only=True)
+            else:
+                print("     !! does not exist !!")
+    except TypeError:
+        print(" --> " + "no LinkSources")
+
+    workbook.api.RefreshAll()
+    workbook.save()
+    workbook.close()
+    excel.quit()

+ 3 - 1
requirements.txt

@@ -2,6 +2,7 @@ beautifulsoup4
 crontab
 flask>=2.2.0
 jinja2
+ldap3
 lxml
 numpy
 openpyxl
@@ -18,4 +19,5 @@ requests_toolbelt
 schedule
 sqlalchemy
 uptime
-xlrd
+xlrd
+xlwings

+ 1 - 1
xls.py

@@ -1,4 +1,4 @@
-import config
+# import config
 import excel
 import typer