Excel Export in eigene Methode und eigenen Schalter ausgelagert

This commit is contained in:
2025-05-22 13:20:47 +02:00
parent af7281f40a
commit c703dc9902
+35 -23
View File
@@ -43,14 +43,8 @@ def add_polyline(msp, points:Polyline, dxf_attribs):
def new_dxf(json_file, out_path, dxf_file=False):
""" creates a new dxf file with a polyline inside which is created by the given json file
"""
with open(json_file, encoding='utf-8') as fh:
data = json.load(fh)
anlage = from_dict(
data_class=Polylines,
data=data
)
#print(anlage)
print("creating dxf ..")
anlage = model_from_json(json_file)
if dxf_file:
# bestehende DXF-Datei laden
@@ -73,22 +67,26 @@ def new_dxf(json_file, out_path, dxf_file=False):
add_polyline(msp, pl, dxfattribs)
doc.saveas(out_path)
print("done")
def model_from_json(json_file):
with open(json_file, encoding='utf-8') as fh:
data = json.load(fh)
anlage = from_dict(
data_class=Polylines,
data=data
)
return anlage
def export_excel(json_file, out_path):
# Hier für Excel Export
excel_path = out_path.replace(".dxf", "_kabellaengen.xlsx")
write_excel_from_json(anlage, excel_path)
print("creating excel file ..")
anlage = model_from_json(json_file)
write_excel_from_json(anlage, out_path)
print("done")
def check_file_in_work(work_dir, filename):
fexists = True
if not os.path.exists(filename):
mypath = os.path.join(work_dir, filename)
if not os.path.exists(mypath):
fexists = False
else:
mypath = filename
return (mypath, fexists)
def write_excel_from_json(anlage:Polylines, outpath:str):
wb = Workbook()
length_summary = defaultdict(int)
@@ -114,12 +112,23 @@ def write_excel_from_json(anlage:Polylines, outpath:str):
wb.save(outpath)
def check_file_in_work(work_dir, filename):
fexists = True
if not os.path.exists(filename):
mypath = os.path.join(work_dir, filename)
if not os.path.exists(mypath):
fexists = False
else:
mypath = filename
return (mypath, fexists)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='draws a dxf file with the given cable coordinates', prog='drawdxf')
parser.add_argument('-f', '--filename', action='store', required=True, help='this json file contains all cables and its coordinates which should be drawn. Saved with an unique timestamp', metavar='myfile.json')
parser.add_argument('-d', '--dxf', action='store', help='this dxf drawing will be copied and the new layer with the cables will be added', metavar='myfile.dxf')
parser.add_argument('-n', '--new', action='store', help='create a new dxf file with cables in it. Name is basename and a timtestamp')
parser.add_argument('-n', '--new', action='store', help='create a new dxf file with cables in it. Name is basename and a timestamp')
parser.add_argument('-x', '--excel', action='store', help='create a xlsx file with cables data', metavar='allCables.xls')
args = parser.parse_args()
@@ -147,6 +156,9 @@ if __name__ == '__main__':
#timestamp = datetime.now().strftime(basename+"_%Y%m%d-%H%M%S.dxf")
#timestamp = datetime.now().strftime("%Y%m%d-%H%M%S.dxf")
out_path = os.path.join(work_dir, args.new)
res_pos = new_dxf(json_path, out_path)
if args.excel:
excel_path = os.path.join(work_dir, args.excel)
export_excel(json_path, excel_path)