i have a code where i was reading the Excel file from Perforce and storing it to the local.
then doing some other work like:
-- read all sheets
-- search for particular columns and extract that column data.
-- and from that data extract the other info from JIRA.
Till here its working fine so once we got all the data we will create a dataframe and then search for the column "STATUS" if there update the column with same data otherwise create a column in the same sheet and write the data to the column.
Code:
import os
import pandas as pd
from jira import JIRA
from pandas import ExcelWriter
from openpyxl import load_workbook
def getStatus(issueID):
jiraURL='http://in-jira-test:0000' #Test server
options = {'server': jiraURL}
jira = JIRA(options, basic_auth=(userName, password))
"""Getting the status for the particular issueID"""
issue = jira.issue(issueID)
status = issue.fields.status
return status
def getFileFromPerforce():
"""
Getting the file from perforce
"""
p4File = ' "//depot/Planning/Configurations.xlsx" '
p4Localfile = "C:/depot/Planning/Configurations.xlsx"
global p4runcmd
p4runcmd = p4Cmd + " sync -f " + p4File
stream = os.popen(p4runcmd)
output = stream.read()
print(output)
return p4File, p4Localfile
def excelReader():
# function call to get the filepath
p4FileLocation, filePath = getFileFromPerforce()
xls=pd.ExcelFile(filePath)
# gets the all sheets names in a list
sheetNameList = xls.sheet_names
for sheets in sheetNameList:
data=pd.read_excel(filePath,sheet_name=sheets)
# Checking the Jira column availability in all sheets
if any("Jira" in columnName for columnName in data.columns):
Value = data['Jira']
colValue=Value.to_frame()
# Getting the status of particular jira issue and updating to the dataframe
for row,rowlen in zip(colValue.iterrows(), range(len(colValue))):
stringData=row[1].to_string()
# getting the issueID from the jira issue url
issueID = stringData.partition('/')[2].rsplit('/')[3]
status = getStatus(issueID)
# data.set_value(k, 'Status', status) #---> deprecated
data.at[rowlen, "Status"]=status
# writting the data to the same excel sheet
print("filePath-",filePath)
excelBook = load_workbook(filePath)
with ExcelWriter(filePath, engine='openpyxl') as writer:
# Save the file workbook as base
writer.book = excelBook
writer.sheets = dict((ws.title, ws) for ws in excelBook.worksheets)
# Creating the new column Status and writing to the sheet which having jira column
data.to_excel(writer, sheets, index=False)
# Save the file
writer.save()
else:
continue
if __name__ == '__main__':
# read userName and passwrod from account file
f = open("account.txt", "r")
lines = f.readlines()
userName = str(lines[0].rstrip())
password = str(lines[1].rstrip())
AdminUser = str(lines[2].rstrip())
AdminPassword = str(lines[3].rstrip())
p4Cmd = 'p4 -c snehil_tool -p indperforce:1444 -u %s -P %s '%(AdminUser,AdminPassword)
f.close
excelReader()
In this code i'm not able to write the data inside the file which i have checkout from perforce i was getting the error :
Traceback (most recent call last):
File "C:/Users/snsingh/PycharmProjects/DemoProgram/JiraStatusUpdate/updateStatusInOpticalFile.py", line 105, in <module>
excelReader()
File "C:/Users/snsingh/PycharmProjects/DemoProgram/JiraStatusUpdate/updateStatusInOpticalFile.py", line 88, in excelReader
writer.save()
File "C:\Users\snsingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 779, in __exit__
self.close()
File "C:\Users\snsingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 783, in close
return self.save()
File "C:\Users\snsingh\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_openpyxl.py", line 44, in save
return self.book.save(self.path)
File "C:\Users\snsingh\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Users\snsingh\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\snsingh\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1204, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:/depot/Planning/Configurations.xlsx'
This piece of code is not working from the above code:
# writting the data to the same excel sheet
print("filePath-",filePath)
excelBook = load_workbook(filePath)
with ExcelWriter(filePath, engine='openpyxl') as writer:
# Save the file workbook as base
writer.book = excelBook
writer.sheets = dict((ws.title, ws) for ws in excelBook.worksheets)
# Creating the new column Status and writing to the sheet which having jira column
data.to_excel(writer, sheets, index=False)
# Save the file
writer.save()
NOTE:
This code works fine with local file contain the same data and its able to write perfectly. but its only happens when i readed the file from perforce.
Even i have given all the permission to the folder and tried with different folder path but i got the same error .Please tell me where i'm making mistake any help would be grate or any questions please fill free to write in comment.
thanks