|
|
|
|
|
"""Google Sheet to XML.ipynb |
|
|
|
|
|
Automatically generated by Colaboratory. |
|
|
|
|
|
Original file is located at |
|
|
https://colab.research.google.com/drive/1T-b1N8Gq6wwbBurODzJIdhRQNeRbyBnz |
|
|
""" |
|
|
|
|
|
from googleapiclient.discovery import build |
|
|
from google.oauth2 import service_account |
|
|
import pygsheets |
|
|
import ast |
|
|
import re |
|
|
import pandas as pd |
|
|
from google.oauth2 import service_account |
|
|
from googleapiclient.discovery import build |
|
|
import pygsheets |
|
|
import tsadropboxretrieval |
|
|
import requests |
|
|
import fitz |
|
|
import numpy as np |
|
|
from PyPDF2 import PdfReader, PdfWriter |
|
|
from io import BytesIO |
|
|
from PyPDF2.generic import NameObject, DictionaryObject, FloatObject, TextStringObject |
|
|
import dropbox |
|
|
|
|
|
|
|
|
def authorizeLegend(): |
|
|
SCOPES = [ |
|
|
'https://www.googleapis.com/auth/spreadsheets', |
|
|
'https://www.googleapis.com/auth/drive', |
|
|
'https://www.googleapis.com/auth/drive.metadata' |
|
|
] |
|
|
credentials = service_account.Credentials.from_service_account_file('credentials.json', scopes=SCOPES) |
|
|
spreadsheet_service = build('sheets', 'v4', credentials=credentials) |
|
|
drive_service = build('drive', 'v3', credentials=credentials) |
|
|
gc = pygsheets.authorize(custom_credentials=credentials, client_secret='credentials.json') |
|
|
return spreadsheet_service,drive_service,gc |
|
|
|
|
|
|
|
|
global gc |
|
|
global pdfpath0 |
|
|
global pdftitle |
|
|
global worksheetw |
|
|
spreadsheet_service,drive_service,gc=authorizeLegend() |
|
|
|
|
|
def create_new_sheet(path): |
|
|
spreadsheet_details = { |
|
|
'properties': { |
|
|
'title': path |
|
|
} |
|
|
} |
|
|
sheet = spreadsheet_service.spreadsheets().create(body=spreadsheet_details, fields='spreadsheetId').execute() |
|
|
spreadsheetId = sheet.get('spreadsheetId') |
|
|
permission1 = { |
|
|
'type': 'anyone', |
|
|
'role': 'writer', |
|
|
} |
|
|
drive_service.permissions().create(fileId=spreadsheetId, body=permission1, supportsAllDrives=True).execute() |
|
|
return spreadsheetId |
|
|
|
|
|
|
|
|
def update_sheet(spreadsheetId, SimilarAreaDictionary, pdfpath,pdf_content=0): |
|
|
ws = gc.open_by_key(spreadsheetId) |
|
|
global pdfpath0 |
|
|
pdfpath0=pdfpath |
|
|
worksheet = ws.worksheet(0) |
|
|
worksheet.title = 'Legend and data created' |
|
|
worksheet.clear() |
|
|
ws.create_developer_metadata('path', pdfpath) |
|
|
|
|
|
if pdf_content: |
|
|
splittedpdfpath = re.split(r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,/<>?]', pdfpath) |
|
|
section=splittedpdfpath[-2] |
|
|
else: |
|
|
section=pdfpath |
|
|
print(section) |
|
|
if section.startswith('2.2') or section.startswith('2.1'): |
|
|
worksheet.set_dataframe(start='A1', df=SimilarAreaDictionary) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
top_header_format = [ |
|
|
{'mergeCells': { |
|
|
'mergeType': 'MERGE_ROWS', |
|
|
'range': { |
|
|
'sheetId': '0', |
|
|
'startRowIndex': 1, |
|
|
'endRowIndex': 2, |
|
|
'startColumnIndex': 3, |
|
|
'endColumnIndex': 5 |
|
|
} |
|
|
}}, |
|
|
{'mergeCells': { |
|
|
'mergeType': 'MERGE_ROWS', |
|
|
'range': { |
|
|
'sheetId': '0', |
|
|
'startRowIndex': 1, |
|
|
'endRowIndex': 2, |
|
|
'startColumnIndex': 5, |
|
|
'endColumnIndex': 7 |
|
|
} |
|
|
}}, |
|
|
{'mergeCells': { |
|
|
'mergeType': 'MERGE_ROWS', |
|
|
'range': { |
|
|
'sheetId': '0', |
|
|
'startRowIndex': 1, |
|
|
'endRowIndex': 2, |
|
|
'startColumnIndex': 7, |
|
|
'endColumnIndex': 9 |
|
|
} |
|
|
}}, |
|
|
{'mergeCells': { |
|
|
'mergeType': 'MERGE_ROWS', |
|
|
'range': { |
|
|
'sheetId': '0', |
|
|
'startRowIndex': 0, |
|
|
'endRowIndex': 1, |
|
|
'startColumnIndex': 0, |
|
|
'endColumnIndex': 11 |
|
|
} |
|
|
}} |
|
|
] |
|
|
spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body={'requests': top_header_format}).execute() |
|
|
worksheet.cell((1, 1)).value = 'Legend and Data Created' |
|
|
worksheet.cell((2, 1)).value = 'Guess' |
|
|
worksheet.cell((2, 2)).value = 'Color' |
|
|
worksheet.cell((2, 3)).value = 'Count' |
|
|
worksheet.cell((2, 4)).value = 'Areas' |
|
|
worksheet.cell((2, 6)).value = 'Perimeters' |
|
|
worksheet.cell((2, 8)).value = 'Lengths' |
|
|
worksheet.cell((2, 10)).value = 'Texts' |
|
|
worksheet.cell((2, 11)).value = 'Comments' |
|
|
second_row_data = ['Nr', 'm2', 'Total', 'm', 'Total', 'm', 'Total'] |
|
|
worksheet.update_row(3, second_row_data, col_offset=2) |
|
|
if len(list(SimilarAreaDictionary['Guess']))>0: |
|
|
worksheet.update_col(1, list(SimilarAreaDictionary['Guess']), row_offset=3) |
|
|
worksheet.update_col(3, list(SimilarAreaDictionary['Occurences']), row_offset=3) |
|
|
worksheet.update_col(4, list(SimilarAreaDictionary['Area']), row_offset=3) |
|
|
worksheet.update_col(5, list(SimilarAreaDictionary['Total Area']), row_offset=3) |
|
|
worksheet.update_col(6, list(SimilarAreaDictionary['Perimeter']), row_offset=3) |
|
|
worksheet.update_col(7, list(SimilarAreaDictionary['Total Perimeter']), row_offset=3) |
|
|
worksheet.update_col(8, list(SimilarAreaDictionary['Length']), row_offset=3) |
|
|
worksheet.update_col(9, list(SimilarAreaDictionary['Total Length']), row_offset=3) |
|
|
worksheet.update_col(10, list(SimilarAreaDictionary['Texts']), row_offset=3) |
|
|
worksheet.update_col(11, list(SimilarAreaDictionary['Comments']), row_offset=3) |
|
|
|
|
|
if section.startswith('1.0'): |
|
|
colorsUsed = [ |
|
|
[SimilarAreaDictionary['R'].iloc[i], SimilarAreaDictionary['G'].iloc[i], SimilarAreaDictionary['B'].iloc[i]] |
|
|
for i in range(len(SimilarAreaDictionary)) |
|
|
] |
|
|
else: |
|
|
colorsUsed = list(SimilarAreaDictionary['Color']) |
|
|
rowsLen = len(SimilarAreaDictionary.values.tolist()) |
|
|
lastcell = worksheet.cell((rowsLen + 2, 1)) |
|
|
lastcellNotation = str(lastcell.address.label) |
|
|
columnsLen = len(SimilarAreaDictionary.columns.values.tolist()) |
|
|
lastUsedCol = columnsLen + 1 |
|
|
worksheet.adjust_column_width(start=2, end=3) |
|
|
worksheet.adjust_column_width(start=10, end=10) |
|
|
worksheet.adjust_column_width(start=4, end=9, pixel_size=60) |
|
|
startrow = 3 |
|
|
endColindex = 11 |
|
|
endrow = 3 |
|
|
sheetId = '0' |
|
|
batch_requests = [] |
|
|
for i in range(len(colorsUsed)): |
|
|
r, g, b = colorsUsed[i] |
|
|
batch_requests.append({ |
|
|
"updateCells": { |
|
|
"range": { |
|
|
"sheetId": sheetId, |
|
|
"startRowIndex": i + startrow, |
|
|
"startColumnIndex": 1, |
|
|
}, |
|
|
"rows": [ |
|
|
{ |
|
|
"values": [ |
|
|
{ |
|
|
"userEnteredFormat": { |
|
|
"backgroundColor": { |
|
|
"red": r / 255, |
|
|
"green": g / 255, |
|
|
"blue": b / 255, |
|
|
"alpha": 0.4, |
|
|
} |
|
|
} |
|
|
} |
|
|
] |
|
|
} |
|
|
], |
|
|
"fields": "userEnteredFormat.backgroundColor", |
|
|
} |
|
|
}) |
|
|
batch_requests.append({ |
|
|
"updateBorders": { |
|
|
"range": { |
|
|
"sheetId": sheetId, |
|
|
"startRowIndex": 0, |
|
|
"endRowIndex": len(SimilarAreaDictionary) + endrow, |
|
|
"startColumnIndex": 0, |
|
|
"endColumnIndex": endColindex |
|
|
}, |
|
|
"top": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green": 0.0, |
|
|
"blue": 0.0 |
|
|
} |
|
|
}, |
|
|
"bottom": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green": 0.0, |
|
|
"blue": 0.0 |
|
|
} |
|
|
}, |
|
|
"left": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green": 0.0, |
|
|
"blue": 0.0 |
|
|
} |
|
|
}, |
|
|
"right": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green": 0.0, |
|
|
"blue": 0.0 |
|
|
} |
|
|
}, |
|
|
"innerHorizontal": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green": 0.0, |
|
|
"blue": 0.0 |
|
|
} |
|
|
}, |
|
|
"innerVertical": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green": 0.0, |
|
|
"blue": 0.0 |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheetId, body={'requests': batch_requests}).execute() |
|
|
|
|
|
model_cell = worksheet.cell('A1') |
|
|
model_cell.set_text_format('bold', True) |
|
|
model_cell.set_horizontal_alignment(pygsheets.custom_types.HorizontalAlignment.CENTER) |
|
|
model_cell.color = (213 / 255, 219 / 255, 255 / 255) |
|
|
pygsheets.DataRange('A2', 'K2', worksheet=worksheet).apply_format(model_cell) |
|
|
|
|
|
|
|
|
def legendGoogleSheets(SimilarAreaDictionary, path, pdfpath, spreadsheetId=0,pdf_content=0): |
|
|
titles = gc.spreadsheet_titles() |
|
|
if path in titles: |
|
|
ws = gc.open(path) |
|
|
global pdftitle |
|
|
pdftitle=path |
|
|
spreadsheetId = ws.id |
|
|
else: |
|
|
spreadsheetId = create_new_sheet(path) |
|
|
ws=gc.open_by_key(spreadsheetId) |
|
|
global worksheetw |
|
|
worksheetw = ws.worksheet(0) |
|
|
if pdf_content: |
|
|
update_sheet(spreadsheetId, SimilarAreaDictionary, pdfpath,pdf_content) |
|
|
else: |
|
|
update_sheet(spreadsheetId, SimilarAreaDictionary, pdfpath) |
|
|
spreadsheet_url = f"https://docs.google.com/spreadsheets/d/{spreadsheetId}" |
|
|
drive_service.permissions().update(transferOwnership=True, fileId=spreadsheetId, permissionId='11OfoB4Z6wOVII8mYmbnCbbqTQs7rYA65') |
|
|
|
|
|
namepathArr = [path, spreadsheetId, ws.get_developer_metadata('path')[0].value] |
|
|
return gc, spreadsheet_service, spreadsheetId, spreadsheet_url, namepathArr |
|
|
|
|
|
|
|
|
def mapnametoLegend(McTName,colorsused,pdflink): |
|
|
|
|
|
sectionKey = McTName.pop() |
|
|
key=sectionKey[0] |
|
|
section=sectionKey[1] |
|
|
|
|
|
|
|
|
spreadsheet_key =str(key) |
|
|
|
|
|
ws = gc.open_by_key(spreadsheet_key) |
|
|
|
|
|
|
|
|
sheetnames=[] |
|
|
unit='' |
|
|
|
|
|
for i in ws._sheet_list: |
|
|
|
|
|
sheetnames.append(i.title) |
|
|
|
|
|
if 'XML Export Summary' in sheetnames: |
|
|
worksheetS = ws.worksheet_by_title('XML Export Summary') |
|
|
else: |
|
|
ws.add_worksheet("XML Export Summary") |
|
|
global worksheetw |
|
|
worksheetw = ws.worksheet(0) |
|
|
worksheetS = ws.worksheet_by_title('XML Export Summary') |
|
|
summaryId= ws[1].id |
|
|
|
|
|
worksheetS.clear() |
|
|
|
|
|
countnames=0 |
|
|
row0=['MC_T Name','Qty','Unit'] |
|
|
worksheetS.update_row(1,row0) |
|
|
|
|
|
allrows=[] |
|
|
for i in range(len(McTName)): |
|
|
allgbnames='' |
|
|
item='' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if McTName[i][2].startswith('Area'): |
|
|
if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'): |
|
|
rowvalue=5 |
|
|
ar=0 |
|
|
unit='m2' |
|
|
if McTName[i][2].startswith('Perimeter'): |
|
|
if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'): |
|
|
rowvalue=7 |
|
|
ar=0 |
|
|
unit='m' |
|
|
if McTName[i][2].startswith('Length'): |
|
|
if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'): |
|
|
rowvalue=9 |
|
|
ar=0 |
|
|
unit='m' |
|
|
if McTName[i][2].startswith('Count'): |
|
|
if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'): |
|
|
rowvalue=3 |
|
|
|
|
|
ar=0 |
|
|
unit='Nr' |
|
|
|
|
|
if isinstance(McTName[i][1], list): |
|
|
guessednames=worksheetw.get_col(1, returnas='matrix', include_tailing_empty=False) |
|
|
|
|
|
for m in McTName[i][1]: |
|
|
if m: |
|
|
if m.startswith('text1'): |
|
|
name=m.removeprefix('text1') |
|
|
|
|
|
allgbnames+= name +' + ' |
|
|
indices = [o for o, x in enumerate(guessednames) if x == name] |
|
|
|
|
|
for j in range(len(indices)): |
|
|
|
|
|
ar+=float(worksheetw.cell((indices[j]+1 ,rowvalue)).value) |
|
|
else: |
|
|
if section.startswith('3.2') or section.startswith('3.3'): |
|
|
item+=m |
|
|
else: |
|
|
item+=m + ' ,' |
|
|
|
|
|
if section.startswith('3.2') or section.startswith('3.3'): |
|
|
n= McTName[i][0] + ' ( '+ allgbnames[:-2]+ ' ) ' |
|
|
else: |
|
|
n= McTName[i][0] + ' ( '+ allgbnames[:-2] +' , ' + item[:-1] + ' ) ' |
|
|
else: |
|
|
if McTName[i][1].startswith('text1'): |
|
|
name=McTName[i][1].removeprefix('text1') |
|
|
allgbnames+= name |
|
|
|
|
|
roww=worksheetw.find(name) |
|
|
for j in range(len(roww)): |
|
|
|
|
|
ar+=float(worksheetw.cell((roww[j].row ,rowvalue)).value) |
|
|
n= McTName[i][0] + ' ( '+ allgbnames + ' ) ' |
|
|
|
|
|
rowi=[str(n),ar,unit] |
|
|
|
|
|
allrows.append([rowi[0],unit]) |
|
|
|
|
|
worksheetS.update_row(i+2,rowi) |
|
|
|
|
|
worksheetS.adjust_column_width(start=1,end=1, pixel_size=350) |
|
|
worksheetS.adjust_column_width(start=2,end=2, pixel_size=100) |
|
|
worksheetS.adjust_column_width(start=3,end=3) |
|
|
guessednamesfinal=getguessnames(worksheetw,colorsused) |
|
|
mctname_guessedNames=label_MC_Tname(allrows) |
|
|
mappedMct_colors=mctname_colors(guessednamesfinal,mctname_guessedNames) |
|
|
adjustannotations(pdflink, mappedMct_colors) |
|
|
|
|
|
xx=(worksheetS.cell( ( len(McTName) +1 ,3)) ).address.label |
|
|
model_cell1 =worksheetS.cell('A2') |
|
|
model_cell1.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.LEFT ) |
|
|
pygsheets.DataRange('A2', str(xx), worksheet=worksheetS).apply_format(model_cell1) |
|
|
|
|
|
|
|
|
model_cell =worksheetS.cell('A1') |
|
|
model_cell.set_text_format('bold', True) |
|
|
model_cell.set_horizontal_alignment( pygsheets.custom_types.HorizontalAlignment.CENTER ) |
|
|
pygsheets.DataRange('A1','C1', worksheet=worksheetS).apply_format(model_cell) |
|
|
|
|
|
body2={ |
|
|
"requests": [ |
|
|
{ |
|
|
"updateBorders": { |
|
|
"range": { |
|
|
"sheetId": str(summaryId), |
|
|
"startRowIndex": 0, |
|
|
"endRowIndex": len(McTName) +1 , |
|
|
"startColumnIndex": 0, |
|
|
"endColumnIndex": 3 |
|
|
}, |
|
|
"top": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"bottom": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"left":{ |
|
|
"style": "SOLID", |
|
|
"width":2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"right":{ |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"innerHorizontal":{ |
|
|
"style": "SOLID", |
|
|
"width":2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"innerVertical": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
] |
|
|
} |
|
|
spreadsheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_key, body=body2).execute() |
|
|
return summaryId |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def label_MC_Tname(mctname_names): |
|
|
|
|
|
mctname_guessedNames=[] |
|
|
|
|
|
for row in mctname_names: |
|
|
text=row[0] |
|
|
unit=row[1] |
|
|
|
|
|
text_before_parenthesis = text.split('(')[0].strip() |
|
|
|
|
|
inside_parentheses = re.search(r'\((.*?)\)', text) |
|
|
if inside_parentheses: |
|
|
inside_parentheses = inside_parentheses.group(1).strip() |
|
|
if '+' in inside_parentheses: |
|
|
|
|
|
parts = [part.strip() for part in inside_parentheses.split('+')] |
|
|
else: |
|
|
|
|
|
parts = [inside_parentheses] |
|
|
mctname_guessedNames.append([text_before_parenthesis,parts , unit]) |
|
|
|
|
|
return mctname_guessedNames |
|
|
|
|
|
|
|
|
def mctname_colors(color_dict, mctname_gb): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
color_dict_map = {color[0].strip(): color[1] for color in color_dict} |
|
|
|
|
|
|
|
|
|
|
|
result = [] |
|
|
|
|
|
|
|
|
for item in mctname_gb: |
|
|
name = item[0] |
|
|
colors = item[1] |
|
|
unit=item[2] |
|
|
|
|
|
|
|
|
color_values = [] |
|
|
for color in colors: |
|
|
trimmed_color = color.strip() |
|
|
if trimmed_color in color_dict_map: |
|
|
color_values.append(color_dict_map[trimmed_color]) |
|
|
else: |
|
|
|
|
|
color_values.append(None) |
|
|
|
|
|
|
|
|
result.append([name, color_values,unit]) |
|
|
|
|
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getguessnames(worksheetw,colorsused): |
|
|
guessednamesfinal=[] |
|
|
|
|
|
guessednames=worksheetw.get_col(1, returnas='matrix', include_tailing_empty=False) |
|
|
|
|
|
for i in range(len(guessednames[3:])): |
|
|
item = guessednames[3:][i] |
|
|
|
|
|
colorforitem = colorsused[i] |
|
|
if item not in guessednamesfinal: |
|
|
guessednamesfinal.append([item, colorforitem]) |
|
|
|
|
|
return guessednamesfinal |
|
|
|
|
|
|
|
|
def is_color_within_tolerance(color1, color2, tolerance): |
|
|
|
|
|
color1 = tuple(map(int, color1)) |
|
|
color2 = tuple(map(int, color2)) |
|
|
|
|
|
return all(abs(c1 - c2) <= tolerance for c1, c2 in zip(color1, color2)) |
|
|
|
|
|
|
|
|
def deletefromlegend(deletedrows, SimilarAreaDictionarycopy, section, areaPermArr=[]): |
|
|
items = [] |
|
|
|
|
|
idx = 0 |
|
|
if section.startswith('1.0') or section.startswith('3.2') or section.startswith('3.3'): |
|
|
areaPermArr = ast.literal_eval(areaPermArr) |
|
|
print(areaPermArr) |
|
|
myDict = eval(SimilarAreaDictionarycopy) |
|
|
SimilarAreaDictionarycopy = pd.DataFrame(myDict) |
|
|
strings = deletedrows['content'] |
|
|
print('content',deletedrows) |
|
|
colors = deletedrows['color'] |
|
|
indicies_toDelete=[] |
|
|
|
|
|
|
|
|
tolerance = 2 |
|
|
|
|
|
color_list = list(SimilarAreaDictionarycopy['Color']) |
|
|
if section.startswith('1.0'): |
|
|
|
|
|
color_list = [ast.literal_eval(color) for color in color_list] |
|
|
|
|
|
for j in range(len(colors)): |
|
|
|
|
|
|
|
|
color = tuple(colors[j]) |
|
|
found = False |
|
|
|
|
|
for idx, existing_color in enumerate(color_list): |
|
|
existing_color = tuple(existing_color) |
|
|
|
|
|
|
|
|
|
|
|
if is_color_within_tolerance(existing_color, color, tolerance): |
|
|
print(f'Color {color} found close to {existing_color} at index {idx}') |
|
|
found = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
comment = SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Comments')] |
|
|
occ = SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')] |
|
|
|
|
|
|
|
|
if "Area" in deletedrows['subject'][j]: |
|
|
matchA = re.search(r"(\d+\.\d+)\s*sq\s*m",strings[j]) |
|
|
print('matchA') |
|
|
|
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Area')] =SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Area')] - float(matchA.group(1)) |
|
|
|
|
|
if pd.notna(comment) and 'Area' in str(comment): |
|
|
matches = re.findall(r'\b\d+\b', str(comment)) |
|
|
area_occurrences = int(matches[0]) - 1 |
|
|
perimeter_occurrences = int(matches[1]) |
|
|
else: |
|
|
area_occurrences = int(SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')]) - 1 |
|
|
perimeter_occurrences = SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')] |
|
|
|
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Comments')] = f'Area occurrences: {area_occurrences}, Perimeter occurrences: {perimeter_occurrences}' |
|
|
if "Perimeter" in deletedrows['subject'][j]: |
|
|
matchP = re.search(r"(\d+\.\d+)\s*m(?![a-zA-Z])", strings[j]) |
|
|
print('matchP') |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Perimeter')] =SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Perimeter')] - float(matchP.group(1)) |
|
|
if pd.notna(comment) and 'Perimeter' in str(comment): |
|
|
matches = re.findall(r'\b\d+\b', str(comment)) |
|
|
area_occurrences = int(matches[0]) |
|
|
perimeter_occurrences = int(matches[1]) -1 |
|
|
else: |
|
|
|
|
|
area_occurrences = int(SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')]) |
|
|
perimeter_occurrences = SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')]-1 |
|
|
|
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Comments')] = f'Area occurrences: {area_occurrences}, Perimeter occurrences: {perimeter_occurrences}' |
|
|
else: |
|
|
matchL = re.search(r"(\d+\.\d+)\s*m(?![a-zA-Z])", strings[j]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if area_occurrences==0 and perimeter_occurrences==0: |
|
|
|
|
|
if str(idx) not in indicies_toDelete: |
|
|
indicies_toDelete.append(str(idx)) |
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Comments')] = f'Area occurrences: {area_occurrences}, Perimeter occurrences: {perimeter_occurrences}' |
|
|
if area_occurrences > perimeter_occurrences: |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')] = area_occurrences |
|
|
elif perimeter_occurrences > area_occurrences: |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')] = perimeter_occurrences |
|
|
else: |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Occurences')] = area_occurrences |
|
|
if area_occurrences==1: |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Area')]= SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Area')] |
|
|
if perimeter_occurrences==1: |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Perimeter')]= SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Perimeter')] |
|
|
if area_occurrences==perimeter_occurrences: |
|
|
if section.startswith('1.0'): |
|
|
SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Length')] =SimilarAreaDictionarycopy.iloc[int(idx), SimilarAreaDictionarycopy.columns.get_loc('Total Length')] - float(matchL.group(1)) |
|
|
break |
|
|
|
|
|
if not found: |
|
|
print(f'Color {color} not found within tolerance') |
|
|
|
|
|
|
|
|
SimilarAreaDictionarycopy.drop(index=indicies_toDelete, axis=0, inplace=True) |
|
|
|
|
|
return SimilarAreaDictionarycopy |
|
|
|
|
|
|
|
|
def DoorsLegend(Dictionary,spreadsheetId): |
|
|
print('id',spreadsheetId) |
|
|
global worksheetw |
|
|
worksheetw.clear() |
|
|
top_header_format = [ |
|
|
{'mergeCells': { |
|
|
'mergeType': 'MERGE_ROWS', |
|
|
'range': { |
|
|
'sheetId': '0', |
|
|
'startRowIndex': 0, |
|
|
'endRowIndex': 1, |
|
|
'startColumnIndex': 0, |
|
|
'endColumnIndex': 2 |
|
|
} |
|
|
}} |
|
|
] |
|
|
print('here') |
|
|
|
|
|
worksheetw.cell((1, 1)).value = 'Legend and Data Created' |
|
|
worksheetw.set_dataframe(start='A2', df=Dictionary) |
|
|
print('here1') |
|
|
body2={ |
|
|
"requests": [ |
|
|
{ |
|
|
"updateBorders": { |
|
|
"range": { |
|
|
"sheetId": str(0), |
|
|
"startRowIndex": 0, |
|
|
"endRowIndex": 4, |
|
|
"startColumnIndex": 0, |
|
|
"endColumnIndex": 2 |
|
|
}, |
|
|
"top": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"bottom": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"left":{ |
|
|
"style": "SOLID", |
|
|
"width":2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"right":{ |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"innerHorizontal":{ |
|
|
"style": "SOLID", |
|
|
"width":2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
"innerVertical": { |
|
|
"style": "SOLID", |
|
|
"width": 2, |
|
|
"color": { |
|
|
"red": 0.0, |
|
|
"green":0.0, |
|
|
"blue":0.0 |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
] |
|
|
} |
|
|
print('here2') |
|
|
|
|
|
model_cell = worksheetw.cell('A1') |
|
|
model_cell.set_text_format('bold', True) |
|
|
model_cell.set_horizontal_alignment(pygsheets.custom_types.HorizontalAlignment.CENTER) |
|
|
model_cell.color = (213 / 255, 219 / 255, 255 / 255) |
|
|
pygsheets.DataRange('A2', 'B2', worksheet=worksheetw).apply_format(model_cell) |
|
|
print('here3') |
|
|
return Dictionary |
|
|
|
|
|
|
|
|
def deletedoors(deletedrows,dictionary): |
|
|
|
|
|
items=[] |
|
|
|
|
|
idx=0 |
|
|
myDict=eval(dictionary) |
|
|
df_doors=pd.DataFrame(myDict) |
|
|
strings=deletedrows['content'] |
|
|
print('strings',strings) |
|
|
for item in strings: |
|
|
newitem=str(item).split('\n \n') |
|
|
if 'Single' in str(newitem): |
|
|
current_value = df_doors.loc[df_doors['Type'] == 'Single Doors', 'Quantity'].values[0] |
|
|
new_value = int(current_value) - 1 |
|
|
df_doors.loc[df_doors['Type'] == 'Single Doors', 'Quantity'] = str(new_value) |
|
|
elif 'Double' in str(newitem): |
|
|
current_value = df_doors.loc[df_doors['Type'] == 'Double Doors', 'Quantity'].values[0] |
|
|
new_value = int(current_value) - 1 |
|
|
df_doors.loc[df_doors['Type'] == 'Double Doors', 'Quantity'] = str(new_value) |
|
|
df_doors = df_doors[['Type', 'Quantity']] |
|
|
print(df_doors) |
|
|
return df_doors |
|
|
|
|
|
|
|
|
|
|
|
def deletemarkups(list1, dbPath, path): |
|
|
'''list1 : original markup pdf |
|
|
list2 : deleted markup pdf |
|
|
deletedrows : deleted markups - difference between both dfs |
|
|
''' |
|
|
|
|
|
myDict1 = eval(list1) |
|
|
list1 = pd.DataFrame(myDict1) |
|
|
|
|
|
dbxTeam = tsadropboxretrieval.ADR_Access_DropboxTeam('user') |
|
|
|
|
|
md, res = dbxTeam.files_download(path=dbPath + path) |
|
|
data = res.content |
|
|
doc = fitz.open("pdf", data) |
|
|
|
|
|
|
|
|
list2 = pd.DataFrame(columns=['content', 'id', 'subject', 'color']) |
|
|
|
|
|
for page in doc: |
|
|
|
|
|
for annot in page.annots(): |
|
|
|
|
|
annot_color = annot.colors |
|
|
if annot_color is not None: |
|
|
|
|
|
stroke_color = annot_color.get('stroke') |
|
|
fill_color = annot_color.get('fill') |
|
|
|
|
|
v = 'stroke' if stroke_color else 'fill' |
|
|
color = annot_color.get(v) |
|
|
if color: |
|
|
|
|
|
color_tuple = (int(color[0] * 255), int(color[1] * 255), int(color[2] * 255)) |
|
|
|
|
|
list2.loc[len(list2)] = [annot.info['content'], annot.info['id'], annot.info['subject'], color_tuple] |
|
|
|
|
|
|
|
|
list1['color'] = list1['color'].apply(lambda x: tuple(x) if isinstance(x, list) else x) |
|
|
|
|
|
|
|
|
deletedrows = pd.concat([list1, list2]).drop_duplicates(keep=False) |
|
|
|
|
|
|
|
|
flag = 0 |
|
|
if len(deletedrows) != 0: |
|
|
flag = 1 |
|
|
deletedrows = deletedrows[['content', 'id', 'subject', 'color']] |
|
|
|
|
|
deletedrows = deletedrows.drop(deletedrows.index[deletedrows['content'].str.startswith('Scale')]) |
|
|
else: |
|
|
flag = 0 |
|
|
|
|
|
return deletedrows |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def adjustannotations(pdflink, color_items): |
|
|
|
|
|
pdf_content = None |
|
|
if pdflink and ('http' in pdflink or 'dropbox' in pdflink): |
|
|
|
|
|
|
|
|
if 'dl=0' in pdflink: |
|
|
pdflink = pdflink.replace('dl=0', 'dl=1') |
|
|
|
|
|
|
|
|
response = requests.get(pdflink) |
|
|
pdf_bytes_io = BytesIO(response.content) |
|
|
|
|
|
reader = PdfReader(pdf_bytes_io) |
|
|
writer = PdfWriter() |
|
|
|
|
|
|
|
|
writer.append_pages_from_reader(reader) |
|
|
|
|
|
|
|
|
metadata = reader.metadata |
|
|
writer.add_metadata(metadata) |
|
|
|
|
|
|
|
|
color_dict = {} |
|
|
for item in color_items: |
|
|
label = item[0] |
|
|
colors = item[1] |
|
|
unit = item[2] |
|
|
|
|
|
for color in colors: |
|
|
color_dict[tuple(color)] = (label, unit) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for page_index, page in enumerate(writer.pages): |
|
|
if "/Annots" in page: |
|
|
annotations = page["/Annots"] |
|
|
for annot_index, annot in enumerate(annotations): |
|
|
obj = annot.get_object() |
|
|
|
|
|
|
|
|
if "/C" in obj: |
|
|
color = tuple(obj["/C"]) |
|
|
color = (int(round(color[0] * 255, 1)), int(round(color[1] * 255, 1)), int(round(color[2] * 255, 1))) |
|
|
|
|
|
|
|
|
if color in color_dict: |
|
|
label, unit = color_dict[color] |
|
|
|
|
|
|
|
|
if "/Contents" in obj: |
|
|
if unit == 'm2' and "sq m" in obj["/Contents"]: |
|
|
obj.update({ |
|
|
NameObject("/Label"): TextStringObject(f"{label}") |
|
|
}) |
|
|
elif unit == 'm' and "m" in obj["/Contents"] and "sq" not in obj["/Contents"]: |
|
|
obj.update({ |
|
|
NameObject("/Label"): TextStringObject(f"{label}") |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
output_pdf_io = BytesIO() |
|
|
writer.write(output_pdf_io) |
|
|
output_pdf_io.seek(0) |
|
|
|
|
|
|
|
|
OutputPdfStage2=output_pdf_io.read() |
|
|
doc2 =fitz.open('pdf',OutputPdfStage2) |
|
|
dbPath='/TSA JOBS/ADR Test'+pdfpath0+'Measured Plan/'+pdftitle |
|
|
|
|
|
|
|
|
print("Annotations updated and saved") |
|
|
dbxTeam= tsadropboxretrieval.ADR_Access_DropboxTeam('user') |
|
|
try: |
|
|
|
|
|
dbxTeam.files_upload(doc2.write() ,dbPath, mode=dropbox.files.WriteMode('overwrite')) |
|
|
|
|
|
print(f"File replaced successfully at") |
|
|
except dropbox.exceptions.ApiError as e: |
|
|
print(f"Error replacing the file: {e}") |
|
|
|
|
|
|
|
|
def getallguessednames(): |
|
|
guessednamesfinal = [] |
|
|
|
|
|
global worksheetw |
|
|
guessednames = worksheetw.get_col(1, returnas='matrix', include_tailing_empty=False) |
|
|
if len(guessednames[3:])>0: |
|
|
|
|
|
for item in guessednames[3:]: |
|
|
if item.strip() and item not in guessednamesfinal: |
|
|
guessednamesfinal.append(item) |
|
|
print('guessednamesfinal:', guessednamesfinal) |
|
|
return guessednamesfinal |
|
|
else: |
|
|
return 'None' |