Marthee commited on
Commit
d9899b7
·
verified ·
1 Parent(s): 7c85b2c

Upload 2 files

Browse files
Files changed (2) hide show
  1. google_sheet_Legend.py +588 -0
  2. google_sheet_to_xml.py +165 -0
google_sheet_Legend.py ADDED
@@ -0,0 +1,588 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Google Sheet to XML.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1T-b1N8Gq6wwbBurODzJIdhRQNeRbyBnz
8
+ """
9
+
10
+ from googleapiclient.discovery import build
11
+ from google.oauth2 import service_account
12
+ import pygsheets
13
+ import ast
14
+ import re
15
+ import pandas as pd
16
+
17
+ def authorizeLegend():
18
+ SCOPES = [
19
+ 'https://www.googleapis.com/auth/spreadsheets',
20
+ 'https://www.googleapis.com/auth/drive',
21
+ 'https://www.googleapis.com/auth/drive.metadata'
22
+ ]
23
+ credentials = service_account.Credentials.from_service_account_file('credentials.json', scopes=SCOPES)
24
+ spreadsheet_service = build('sheets', 'v4', credentials=credentials)
25
+ drive_service = build('drive', 'v3', credentials=credentials)
26
+ gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json')
27
+ return spreadsheet_service,drive_service,gc
28
+
29
+
30
+ def legendGoogleSheets(SimilarAreaDictionary,path ,pdfpath, spreadsheetId=0):
31
+
32
+ spreadsheet_service,drive_service,gc=authorizeLegend()
33
+ ########
34
+ legendTitle= path
35
+ titles=gc.spreadsheet_titles()
36
+
37
+
38
+
39
+ if legendTitle in titles:
40
+ print('found sheet ', legendTitle)
41
+ ws=gc.open(str(legendTitle))
42
+ spreadsheetId=ws.id
43
+ else:
44
+ # ####### create new sheet
45
+ print('creating new sheeet')
46
+
47
+ spreadsheet_details = {
48
+ 'properties': {
49
+ 'title': path
50
+ }
51
+ }
52
+ sheet = spreadsheet_service.spreadsheets().create(body=spreadsheet_details,
53
+ fields='spreadsheetId').execute()
54
+
55
+ spreadsheetId = sheet.get('spreadsheetId')
56
+ permission1 = {
57
+ 'type': 'anyone',
58
+ 'role': 'writer',
59
+ # 'emailAddress': 'marthe.adr@gmail.com'
60
+ }
61
+ # permission2 = {
62
+ # 'type': 'user',
63
+ # 'role': 'writer',
64
+ # 'emailAddress': 'marthe.adr@gmail.com',
65
+ # 'pendingOwner': True
66
+ # }
67
+
68
+
69
+ drive_service.permissions().create(fileId=spreadsheetId, body=permission1, supportsAllDrives=True ).execute()
70
+ ws=gc.open_by_key(spreadsheetId)
71
+
72
+ sheetId = '0' # Please set sheet ID.
73
+ worksheet = ws.worksheet(0)
74
+ worksheet.title='Legend and data created'
75
+ worksheet.clear()
76
+ print('PDFPATHHH',pdfpath)
77
+ ws.create_developer_metadata('path',pdfpath)
78
+ splittedpdfpath=re.split(r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,/<>?]', pdfpath)
79
+ namepathArr=[legendTitle , spreadsheetId,ws.get_developer_metadata('path')[0].value]
80
+ if splittedpdfpath[-2].startswith('2.2') or splittedpdfpath[-2].startswith('2.1') :
81
+ worksheet.set_dataframe(start='A1',df=SimilarAreaDictionary)
82
+ print(SimilarAreaDictionary)
83
+ else:
84
+ top_header_format = [
85
+ {'mergeCells': { #areas
86
+ 'mergeType': 'MERGE_ROWS',
87
+ 'range': {
88
+ 'sheetId': '0',
89
+ 'startRowIndex': 1,
90
+ 'endRowIndex': 2,
91
+ 'startColumnIndex': 3,
92
+ 'endColumnIndex':5
93
+
94
+
95
+ }
96
+ }},
97
+
98
+ {'mergeCells': { #perimeters
99
+ 'mergeType': 'MERGE_ROWS',
100
+ 'range': {
101
+ 'sheetId': '0',
102
+ 'startRowIndex': 1,
103
+ 'endRowIndex': 2,
104
+ 'startColumnIndex': 5,
105
+ 'endColumnIndex':7
106
+ }
107
+
108
+ }},
109
+ {'mergeCells': { #lengths
110
+ 'mergeType': 'MERGE_ROWS',
111
+ 'range': {
112
+ 'sheetId': '0',
113
+ 'startRowIndex': 1,
114
+ 'endRowIndex': 2,
115
+ 'startColumnIndex': 7,
116
+ 'endColumnIndex':9
117
+ }
118
+
119
+ }},
120
+
121
+ {'mergeCells': { # legend and data created
122
+ 'mergeType': 'MERGE_ROWS',
123
+ 'range': {
124
+ 'sheetId': '0',
125
+ 'startRowIndex': 0,
126
+ 'endRowIndex': 1,
127
+ 'startColumnIndex': 0,
128
+ 'endColumnIndex':10
129
+ }
130
+
131
+ }}
132
+ ]
133
+
134
+
135
+ spreadsheet_service.spreadsheets().batchUpdate( spreadsheetId=spreadsheetId , body={'requests': top_header_format} ).execute()
136
+ worksheet.cell((1,1)).value='Legend and Data Created'
137
+ worksheet.cell((2,1)).value='Guess'
138
+ worksheet.cell((2,2)).value='Color'
139
+ worksheet.cell((2,3)).value='Count'
140
+ worksheet.cell((2,4)).value='Areas'
141
+ worksheet.cell((2,6)).value='Perimeters'
142
+ worksheet.cell((2,8)).value='Lengths'
143
+ worksheet.cell((2,10)).value='Texts'
144
+ second_row_data=['Nr','m2','Total','m','Total','m','Total']
145
+ if splittedpdfpath[-2].startswith('1.0') or splittedpdfpath[-2].startswith('3.2'):
146
+ worksheet.update_row(3,second_row_data,col_offset=2)
147
+ worksheet.update_col(1,list(SimilarAreaDictionary['Guess']),row_offset=3)
148
+ worksheet.update_col(3,list(SimilarAreaDictionary['Occurences']),row_offset=3)
149
+ worksheet.update_col(4,list(SimilarAreaDictionary['Area']),row_offset=3)
150
+ worksheet.update_col(5,list(SimilarAreaDictionary['Total Area']),row_offset=3)
151
+ worksheet.update_col(6,list(SimilarAreaDictionary['Perimeter']),row_offset=3)
152
+ worksheet.update_col(7,list(SimilarAreaDictionary['Total Perimeter']),row_offset=3)
153
+
154
+ worksheet.update_col(8,list(SimilarAreaDictionary['Length']),row_offset=3)
155
+ worksheet.update_col(9,list(SimilarAreaDictionary['Total Length']),row_offset=3)
156
+ worksheet.update_col(10,list(SimilarAreaDictionary['Texts']),row_offset=3)
157
+ if splittedpdfpath[-2].startswith('1.0'):
158
+ colorsUsed=[]
159
+ for i in range(len(SimilarAreaDictionary)):
160
+ colorsUsed.append([SimilarAreaDictionary['R'].iloc[i] ,SimilarAreaDictionary['G'].iloc[i] , SimilarAreaDictionary['B'].iloc[i]] )
161
+ elif splittedpdfpath[-2].startswith('3.2'):
162
+ colorsUsed=list(SimilarAreaDictionary['Color'])
163
+
164
+ #legend specs here
165
+ rowsLen=len(SimilarAreaDictionary.values.tolist()) #kam row -- last row = rowsLen +1
166
+ lastcell=worksheet.cell((rowsLen+2,1)) #row,col
167
+ lastcellNotation=str(lastcell.address.label)
168
+ # worksheet.set_data_validation('A3',lastcellNotation, condition_type='ONE_OF_LIST', condition_values=['Ground Beam','Pile Cap'], showCustomUi=True)
169
+
170
+ #get lengths of df
171
+ columnsLen=len(SimilarAreaDictionary.columns.values.tolist()) #kam column -- last col = columnsLen+1 3shan base0
172
+ lastUsedCol=columnsLen+1
173
+
174
+ worksheet.adjust_column_width(start=2,end=3)
175
+ worksheet.adjust_column_width(start=10,end=10)
176
+ # if splittedpdfpath[-2].startswith('1.0'):
177
+ worksheet.adjust_column_width(start=4,end=9,pixel_size=60)
178
+ startrow = 3
179
+ # elif splittedpdfpath[-2].startswith('3.2'):
180
+ # startrow=2
181
+
182
+ sheetId = '0' # Please set sheet ID.
183
+ for i in range(len(colorsUsed)):
184
+
185
+ print(colorsUsed[i])
186
+ r,g,b=colorsUsed[i]
187
+ body = {
188
+ "requests": [
189
+ {
190
+ "updateCells": {
191
+ "range": {
192
+ "sheetId": sheetId,
193
+ "startRowIndex": i+startrow,
194
+ # "endRowIndex":4 ,
195
+ "startColumnIndex":1,
196
+
197
+ # "endColumnIndex": 0
198
+ },
199
+
200
+ "rows": [
201
+ {
202
+ "values": [
203
+ {
204
+ "userEnteredFormat": {
205
+ "backgroundColor": {
206
+
207
+ "red": r/255,
208
+ "green": g/255,
209
+ "blue": b/255,
210
+ "alpha": 0.4,
211
+
212
+ }
213
+
214
+ }
215
+ }
216
+ ]
217
+ }
218
+ ],
219
+ "fields": "userEnteredFormat.backgroundColor",
220
+
221
+ }
222
+
223
+
224
+
225
+ }
226
+ ]
227
+ }
228
+ res = spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body).execute()
229
+ # if splittedpdfpath[-2].startswith('1.0'):
230
+ endColindex=10
231
+ endrow=3
232
+ body2={
233
+ "requests": [
234
+ {
235
+ "updateBorders": {
236
+ "range": {
237
+ "sheetId": sheetId,
238
+ "startRowIndex": 0,
239
+ "endRowIndex": len(SimilarAreaDictionary)+endrow,
240
+ "startColumnIndex": 0,
241
+ "endColumnIndex": endColindex
242
+ },
243
+ "top": {
244
+ "style": "SOLID",
245
+ "width": 2,
246
+ "color": {
247
+ "red": 0.0,
248
+ "green":0.0,
249
+ "blue":0.0
250
+ },
251
+ },
252
+ "bottom": {
253
+ "style": "SOLID",
254
+ "width": 2,
255
+ "color": {
256
+ "red": 0.0,
257
+ "green":0.0,
258
+ "blue":0.0
259
+ },
260
+ },
261
+ "left":{
262
+ "style": "SOLID",
263
+ "width":2,
264
+ "color": {
265
+ "red": 0.0,
266
+ "green":0.0,
267
+ "blue":0.0
268
+ },
269
+ },
270
+ "right":{
271
+ "style": "SOLID",
272
+ "width": 2,
273
+ "color": {
274
+ "red": 0.0,
275
+ "green":0.0,
276
+ "blue":0.0
277
+ },
278
+ },
279
+ "innerHorizontal":{
280
+ "style": "SOLID",
281
+ "width":2,
282
+ "color": {
283
+ "red": 0.0,
284
+ "green":0.0,
285
+ "blue":0.0
286
+ },
287
+ },
288
+ "innerVertical": {
289
+ "style": "SOLID",
290
+ "width": 2,
291
+ "color": {
292
+ "red": 0.0,
293
+ "green":0.0,
294
+ "blue":0.0
295
+ },
296
+ },
297
+ }
298
+ }
299
+ ]
300
+ }
301
+ spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body=body2).execute()
302
+
303
+ model_cell =worksheet.cell('A1')
304
+ model_cell.set_text_format('bold', True)
305
+ model_cell.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.CENTER )
306
+ model_cell.color = (213/255, 219/255 ,255/255)
307
+ pygsheets.DataRange('A2','J2', worksheet=worksheet).apply_format(model_cell)
308
+ spreadsheet_url = "https://docs.google.com/spreadsheets/d/%s" % spreadsheetId
309
+ print(spreadsheet_url)
310
+ drive_service.permissions().update(transferOwnership=True , fileId=spreadsheetId,permissionId='11OfoB4Z6wOVII8mYmbnCbbqTQs7rYA65')
311
+
312
+
313
+ return gc,spreadsheet_service,spreadsheetId ,spreadsheet_url , namepathArr
314
+ #######################
315
+
316
+ def mapnametoLegend(McTName):
317
+
318
+ sectionKey = McTName.pop()
319
+ key=sectionKey[0]
320
+ section=sectionKey[1]
321
+
322
+ spreadsheet_service,drive_service,gc=authorizeLegend()
323
+ spreadsheet_key =str(key) # Please set the Spreadsheet ID.
324
+
325
+ ws = gc.open_by_key(spreadsheet_key)
326
+ # guessednamesfinal=getguessnames(gc,ws)
327
+ sheetnames=[]
328
+ unit=''
329
+ # ws.add_worksheet("Summary") # Please set the new sheet name.
330
+ for i in ws._sheet_list:
331
+ print(i)
332
+ sheetnames.append(i.title)
333
+ print(i.index)
334
+ if 'XML Export Summary' in sheetnames:
335
+ worksheetS = ws.worksheet_by_title('XML Export Summary')
336
+ else:
337
+ ws.add_worksheet("XML Export Summary") # Please set the new sheet name.
338
+ worksheetw = ws.worksheet(0) #legend
339
+ worksheetS = ws.worksheet_by_title('XML Export Summary')
340
+ summaryId= ws[1].id
341
+ print('summaryyyID',summaryId)
342
+ print('summaryyyID2',worksheetS.id)
343
+ worksheetS.clear()
344
+
345
+ countnames=0
346
+ row0=['MC_T Name','Qty','Unit']
347
+ worksheetS.update_row(1,row0)
348
+
349
+
350
+ for i in range(len(McTName)):
351
+ allgbnames=''
352
+ item=''
353
+ print(McTName[i][0])
354
+
355
+ # firstpart= re.split(r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,./<>?]', McTName[i][0])
356
+ print('kkk' ,McTName[i][2])
357
+ if McTName[i][2].startswith('Area'):
358
+ if section.startswith('1.0'):
359
+ rowvalue=5# column 5
360
+ elif section.startswith('3.2'):
361
+ rowvalue=3
362
+ ar=0
363
+ unit='m2'
364
+ if McTName[i][2].startswith('Perimeter'):
365
+ if section.startswith('1.0'):
366
+ rowvalue=7# column 7
367
+ elif section.startswith('3.2'):
368
+ rowvalue=3
369
+ ar=0
370
+ unit='m'
371
+ if McTName[i][2].startswith('Length'):
372
+ if section.startswith('1.0'):
373
+ rowvalue=9# column 7
374
+ elif section.startswith('3.2'):
375
+ rowvalue=3
376
+ ar=0
377
+ unit='m'
378
+ if McTName[i][2].startswith('Count'):
379
+ if section.startswith('1.0'):
380
+ rowvalue=3# column 7
381
+ elif section.startswith('3.2'):
382
+ rowvalue=3
383
+ ar=0
384
+ unit='Nr'
385
+
386
+ print('mcct',McTName[i][1])
387
+ if isinstance(McTName[i][1], list):
388
+ guessednames=worksheetw.get_col(1, returnas='matrix', include_tailing_empty=False)
389
+
390
+ for m in McTName[i][1]:
391
+ if m:
392
+ if m.startswith('text1'):
393
+ name=m.removeprefix('text1')
394
+ allgbnames+= name +' +'
395
+ indices = [o for o, x in enumerate(guessednames) if x == name]
396
+ print(indices)
397
+ for j in range(len(indices)):
398
+ # print('kjjjj',roww[j])
399
+ ar+=float(worksheetw.cell((indices[j]+1 ,rowvalue)).value)
400
+ else:
401
+ item+=m + ' ,'
402
+ print(item)
403
+ n= McTName[i][0] + ' ( '+ allgbnames[:-2] +' , ' + item[:-1] + ' ) '
404
+ else:
405
+ if McTName[i][1].startswith('text1'):
406
+ name=McTName[i][1].removeprefix('text1')
407
+ allgbnames+= name
408
+
409
+ roww=worksheetw.find(name)
410
+ print(roww)
411
+ for j in range(len(roww)):
412
+ print('kjjjj',roww[j])
413
+ ar+=float(worksheetw.cell((roww[j].row ,rowvalue)).value)
414
+ n= McTName[i][0] + ' ( '+ allgbnames + ' ) '
415
+
416
+ rowi=[str(n),ar,unit]
417
+ worksheetS.update_row(i+2,rowi)
418
+ # worksheetS.adjust_column_width(start=1,end=4)
419
+ worksheetS.adjust_column_width(start=1,end=1, pixel_size=350)
420
+ worksheetS.adjust_column_width(start=2,end=2, pixel_size=100)
421
+ worksheetS.adjust_column_width(start=3,end=3)
422
+
423
+ xx=(worksheetS.cell( ( len(McTName) +1 ,3)) ).address.label
424
+ model_cell1 =worksheetS.cell('A2')
425
+ model_cell1.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.LEFT )
426
+ pygsheets.DataRange('A2', str(xx), worksheet=worksheetS).apply_format(model_cell1)
427
+
428
+
429
+ model_cell =worksheetS.cell('A1')
430
+ model_cell.set_text_format('bold', True)
431
+ model_cell.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.CENTER )
432
+ pygsheets.DataRange('A1','C1', worksheet=worksheetS).apply_format(model_cell)
433
+
434
+ body2={
435
+ "requests": [
436
+ {
437
+ "updateBorders": {
438
+ "range": {
439
+ "sheetId": str(summaryId),
440
+ "startRowIndex": 0,
441
+ "endRowIndex": len(McTName) +1 ,
442
+ "startColumnIndex": 0,
443
+ "endColumnIndex": 3
444
+ },
445
+ "top": {
446
+ "style": "SOLID",
447
+ "width": 2,
448
+ "color": {
449
+ "red": 0.0,
450
+ "green":0.0,
451
+ "blue":0.0
452
+ },
453
+ },
454
+ "bottom": {
455
+ "style": "SOLID",
456
+ "width": 2,
457
+ "color": {
458
+ "red": 0.0,
459
+ "green":0.0,
460
+ "blue":0.0
461
+ },
462
+ },
463
+ "left":{
464
+ "style": "SOLID",
465
+ "width":2,
466
+ "color": {
467
+ "red": 0.0,
468
+ "green":0.0,
469
+ "blue":0.0
470
+ },
471
+ },
472
+ "right":{
473
+ "style": "SOLID",
474
+ "width": 2,
475
+ "color": {
476
+ "red": 0.0,
477
+ "green":0.0,
478
+ "blue":0.0
479
+ },
480
+ },
481
+ "innerHorizontal":{
482
+ "style": "SOLID",
483
+ "width":2,
484
+ "color": {
485
+ "red": 0.0,
486
+ "green":0.0,
487
+ "blue":0.0
488
+ },
489
+ },
490
+ "innerVertical": {
491
+ "style": "SOLID",
492
+ "width": 2,
493
+ "color": {
494
+ "red": 0.0,
495
+ "green":0.0,
496
+ "blue":0.0
497
+ },
498
+ },
499
+ }
500
+ }
501
+ ]
502
+ }
503
+ spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_key, body=body2).execute()
504
+ return summaryId #,guessednamesfinal
505
+
506
+ # print(x,xarea)
507
+
508
+ def getguessnames(gc,ws):
509
+ guessednamesfinal=[]
510
+ worksheetw = ws.worksheet(0)
511
+ guessednames=worksheetw.get_col(1, returnas='matrix', include_tailing_empty=False)
512
+ print(guessednames[2:])
513
+ for item in guessednames[2:]:
514
+ if item not in guessednamesfinal:
515
+ guessednamesfinal.append(item)
516
+ print(guessednamesfinal)
517
+ return guessednamesfinal
518
+
519
+ ################################################################
520
+
521
+ def deletefromlegend(deletedrows,SimilarAreaDictionarycopy,section, areaPermArr=[]):
522
+ items=[]
523
+ idx=0
524
+ if section.startswith('1.0'):
525
+ areaPermArr=ast.literal_eval(areaPermArr)
526
+ myDict=eval(SimilarAreaDictionarycopy)
527
+ SimilarAreaDictionarycopy=pd.DataFrame(myDict)
528
+ strings=deletedrows['content']
529
+ areastodelete = []
530
+ perimstodelete=[]
531
+ lengthstodelete=[]
532
+ print('areaPermArr',areaPermArr)
533
+ for item in strings:
534
+ items.append(str(item).split('\n \n'))
535
+ # print('itemsssssss',float(re.findall("\d+\.\d+", str(items[i][0]).split()[0])[0])) #take area and perim mn hna l sec 3.2 and +/- value margin
536
+
537
+ for i in range(len(items)):
538
+ print('ITEMSS',str(items[i]).split())
539
+ areastodelete.append(float(re.findall("\d+\.\d+", str(items[i][0]).split()[1])[0]))
540
+ perimstodelete.append(float(re.findall("\d+\.\d+", str(items[i][1]).split()[1])[0]) )
541
+ lengthstodelete.append(float(re.findall("\d+\.\d+", str(items[i][2]).split()[1])[0]) )
542
+
543
+ for i in range(len(areastodelete)):#item in areastodelete:
544
+ areamin=round(areastodelete[i],1)- 0.3
545
+ areamax=round(areastodelete[i],1)+ 0.3
546
+ perimmin=round(perimstodelete[i],1)- 0.3
547
+ perimmax=round(perimstodelete[i],1)+ 0.3
548
+ if section.startswith('1.0'):
549
+ for p in range(len(areaPermArr)):
550
+
551
+ if areastodelete[i] in areaPermArr[p]:
552
+ print('AAA',areaPermArr[p])
553
+ area= areaPermArr[p][0]
554
+ width= areaPermArr[p][1]
555
+ height= areaPermArr[p][2]
556
+ # if section.startswith('1.0'):
557
+ widthMin= width -10
558
+ widthMax= width +10
559
+ heightMin = height-10
560
+ heightMax=height+10
561
+ found=SimilarAreaDictionarycopy.loc[SimilarAreaDictionarycopy.index[((SimilarAreaDictionarycopy['Rounded'] >=areamin) & (SimilarAreaDictionarycopy['Rounded']<=areamax) & (SimilarAreaDictionarycopy['Perimeter'] >=perimmin) & (SimilarAreaDictionarycopy['Perimeter']<=perimmax) ) & ( ((SimilarAreaDictionarycopy['Width']>=widthMin) & (SimilarAreaDictionarycopy['Width']<=widthMax) & (SimilarAreaDictionarycopy['Height']>=heightMin) & (SimilarAreaDictionarycopy['Height']<=heightMax) ) | ((SimilarAreaDictionarycopy['Width']>=heightMin) & (SimilarAreaDictionarycopy['Width']<=heightMax) & (SimilarAreaDictionarycopy['Height']>=widthMin) & (SimilarAreaDictionarycopy['Height']<=widthMax) )) ]]
562
+ elif section.startswith('3.2'):
563
+ areamin=round(areastodelete[i],1)- 0.1
564
+ areamax= round(areastodelete[i],1) + 0.1
565
+
566
+ found=SimilarAreaDictionarycopy.loc[SimilarAreaDictionarycopy.index[((SimilarAreaDictionarycopy['Area'] >=areamin) & (SimilarAreaDictionarycopy['Area']<=areamax) )]]
567
+
568
+ if len(found.index.values ) >0:
569
+ occ=SimilarAreaDictionarycopy.loc[found.index.values[0],'Occurences']
570
+ if occ== 1: #drop row
571
+ print('occ=1')
572
+ SimilarAreaDictionarycopy= SimilarAreaDictionarycopy.drop(found.index.values[0])
573
+
574
+ else: #occ minus 1 , total area - areavalue , total perim - perimvalue
575
+ print('occ>1')
576
+ if section.startswith('1.0'):
577
+ idx=SimilarAreaDictionarycopy.index[((SimilarAreaDictionarycopy['Rounded'] >=areamin) & (SimilarAreaDictionarycopy['Rounded']<=areamax) & (SimilarAreaDictionarycopy['Perimeter'] >=perimmin) & (SimilarAreaDictionarycopy['Perimeter']<=perimmax) ) & ( ((SimilarAreaDictionarycopy['Width']>=widthMin) & (SimilarAreaDictionarycopy['Width']<=widthMax) & (SimilarAreaDictionarycopy['Height']>=heightMin) & (SimilarAreaDictionarycopy['Height']<=heightMax) ) | ((SimilarAreaDictionarycopy['Width']>=heightMin) & (SimilarAreaDictionarycopy['Width']<=heightMax) & (SimilarAreaDictionarycopy['Height']>=widthMin) & (SimilarAreaDictionarycopy['Height']<=widthMax) )) ]
578
+ elif section.startswith('3.2'):
579
+ perimmin=round(perimstodelete[i],1)- 50
580
+ perimmax=round(perimstodelete[i],1)+ 50
581
+ idx=SimilarAreaDictionarycopy.index[((SimilarAreaDictionarycopy['Area'] >=areamin) & (SimilarAreaDictionarycopy['Area']<=areamax) & (SimilarAreaDictionarycopy['Perimeter'] >=perimmin) & (SimilarAreaDictionarycopy['Perimeter']<=perimmax) )]
582
+ SimilarAreaDictionarycopy.loc[idx,'Total Area'] = SimilarAreaDictionarycopy.loc[idx,'Total Area'] - areastodelete[i]
583
+ SimilarAreaDictionarycopy.loc[idx,'Total Perimeter'] = SimilarAreaDictionarycopy.loc[idx,'Total Perimeter'] - perimstodelete[i]
584
+ SimilarAreaDictionarycopy.loc[idx,'Total Length'] = SimilarAreaDictionarycopy.loc[idx,'Total Length'] - lengthstodelete[i]
585
+ SimilarAreaDictionarycopy.loc[idx,'Occurences'] = int(SimilarAreaDictionarycopy.loc[idx,'Occurences']) - 1
586
+ return SimilarAreaDictionarycopy
587
+
588
+ #############################################################
google_sheet_to_xml.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Google Sheet to XML.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1T-b1N8Gq6wwbBurODzJIdhRQNeRbyBnz
8
+ """
9
+
10
+ from googleapiclient.discovery import build
11
+ from google.oauth2 import service_account
12
+ import pygsheets
13
+ import xml.etree.ElementTree as ET
14
+ import tsadropboxretrieval
15
+
16
+ def authorizeLegend():
17
+ SCOPES = [
18
+ 'https://www.googleapis.com/auth/spreadsheets',
19
+ 'https://www.googleapis.com/auth/drive',
20
+ 'https://www.googleapis.com/auth/drive.metadata'
21
+ ]
22
+ credentials = service_account.Credentials.from_service_account_file('credentials.json', scopes=SCOPES)
23
+ spreadsheet_service = build('sheets', 'v4', credentials=credentials)
24
+ drive_service = build('drive', 'v3', credentials=credentials)
25
+ gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json')
26
+ return spreadsheet_service,drive_service,gc
27
+
28
+
29
+ def getTimes(drive_service,spreadsheetId):
30
+ summaryTimes=drive_service.files().get(fileId=spreadsheetId,fields="createdTime, modifiedTime").execute()
31
+ createdTime=summaryTimes.get('createdTime')
32
+ modifiedTime=summaryTimes.get('modifiedTime')
33
+ return createdTime,modifiedTime
34
+
35
+
36
+
37
+ def openSpreadsheet_DFs(legendTitle):
38
+ spreadsheet_service,drive_service,gc=authorizeLegend()
39
+ # legendTitle= '2123-HRW-01-BG-DR-S-110_P.03.pdf'
40
+ print('found sheet ', legendTitle)
41
+ ws=gc.open(str(legendTitle))
42
+ spreadsheetId=ws.id
43
+
44
+ worksheet0 = ws.worksheet(0)
45
+ worksheet1 = ws.worksheet_by_title('XML Export Summary')
46
+
47
+ df0=worksheet0.get_as_df()
48
+ df1=worksheet1.get_as_df()
49
+ createdTime,modifiedTime=getTimes(drive_service,spreadsheetId)
50
+ return df0,df1,createdTime,modifiedTime
51
+
52
+
53
+
54
+
55
+ def create_xml(documentname , dbPath):
56
+ df0,df1,createdTime,modifiedTime=openSpreadsheet_DFs(documentname)
57
+ # createdTime,modifiedTime=getTimes()
58
+ MarkupSummary=ET.Element('<MarkupSummary Version="1.0" Document="'+documentname+'">')
59
+ # we make root element
60
+ MarkupSummary = ET.SubElement(MarkupSummary, "MarkupSummary")
61
+ MarkupSummary.attrib['Version'] = "1.0"
62
+ MarkupSummary.attrib['Document'] = documentname
63
+ # insert list element into sub elements
64
+ for rowX,rowY in df1.iterrows():
65
+ print(rowX,rowY)
66
+ Markup = ET.Element("Markup")
67
+ Markup = ET.SubElement(MarkupSummary, "Markup")
68
+
69
+ Subject = ET.SubElement(Markup, "Subject")
70
+ Subject.text=str(rowY[0])
71
+
72
+ Date = ET.SubElement(Markup, "Date")
73
+ Date.text=str(modifiedTime.split('T')[0].replace('-','/')+ ' '+ modifiedTime.split('T')[1].split('.')[0])
74
+
75
+ Colour = ET.SubElement(Markup, "Colour")
76
+ Colour.text=str(' ')
77
+
78
+ Count = ET.SubElement(Markup, "Count")
79
+ Count.text=str(' ')
80
+
81
+ Author = ET.SubElement(Markup, "Author")
82
+ Author.text=str('ADR Team')
83
+ if rowY[2]=='m':
84
+ Length = ET.SubElement(Markup, "Length")
85
+ Length.text=str(rowY[1])
86
+ else:
87
+ Length = ET.SubElement(Markup, "Length")
88
+ Length.text=str(0)
89
+
90
+ Height = ET.SubElement(Markup, "Height")
91
+ Height.text=str(0)
92
+ if rowY[2]=='m2':
93
+ Area = ET.SubElement(Markup, "Area")
94
+ Area.text=str(rowX[1])
95
+ else:
96
+ Area = ET.SubElement(Markup, "Area")
97
+ Area.text=str(0)
98
+ Label = ET.SubElement(Markup, "Label")
99
+ Label.text=str(rowY[0])
100
+
101
+ Page_Label = ET.SubElement(Markup, "Page_Label")
102
+ Page_Label.text=str(' ')
103
+
104
+ Page_Index = ET.SubElement(Markup, "Page_Index")
105
+ Page_Index.text=str(' ')
106
+
107
+ Lock = ET.SubElement(Markup, "Lock")
108
+ Lock.text=str('Unlocked')
109
+
110
+ Tick_mark = ET.SubElement(Markup, "Tick_mark")
111
+ Tick_mark.text=str(' ')
112
+
113
+ Creation_Date = ET.SubElement(Markup, "Creation_Date")
114
+ Creation_Date.text=str(createdTime.split('T')[0].replace('-','/'))
115
+
116
+ x = ET.SubElement(Markup, "X")
117
+ x.text=str(' ')
118
+
119
+ y= ET.SubElement(Markup, "Y")
120
+ y.text=str(' ')
121
+
122
+ Document_Width= ET.SubElement(Markup, "Document_Width")
123
+ Document_Width.text=str(' ')
124
+
125
+ Document_Height= ET.SubElement(Markup, "Document_Height")
126
+ Document_Height.text=str(' ')
127
+
128
+ RiseDrop= ET.SubElement(Markup, "RiseDrop")
129
+ RiseDrop.text=str(0)
130
+
131
+ Unit= ET.SubElement(Markup, "Unit")
132
+ Unit.text=str(rowY[2])
133
+
134
+ Wall_Area= ET.SubElement(Markup, "Wall_Area")
135
+ Wall_Area.text=str(' ')
136
+
137
+ Depth= ET.SubElement(Markup, "Depth")
138
+ Depth.text=str(0.00)
139
+
140
+ Volume= ET.SubElement(Markup, "Volume")
141
+ Volume.text=str(0)
142
+
143
+ Measurement= ET.SubElement(Markup, "Measurement")
144
+ Measurement.text=str(rowY[1])
145
+
146
+ Layer= ET.SubElement(Markup, "Layer")
147
+ Layer.text=str(' ')
148
+
149
+ Capture= ET.SubElement(Markup, "Capture")
150
+ Capture.text=str('No')
151
+
152
+ File_Name= ET.SubElement(Markup, "File_Name")
153
+ File_Name.text=str(documentname)
154
+
155
+ tree = ET.ElementTree(Markup)
156
+ tree = ET.ElementTree(MarkupSummary)
157
+ # write the tree into an XML file
158
+ # tree.write(str(documentname).split('.pdf')[0]+".xml", encoding ='UTF-8', xml_declaration = True ,method='xml')
159
+ # dbPath='/TSA Team Folder/ADR Test'+pdfpath+'XML/'
160
+ # (pdfname,path)
161
+ # path=path+pdfname
162
+ xmllink= tsadropboxretrieval.uploadanyFile(doc=tree,pdfname=documentname,path=dbPath,flag='xml')
163
+ return xmllink
164
+ # create_xml(legendTitle)
165
+