I have been asked to create a web page with django, with a button in it, which when clicked runs a python script in command prompt in your system. It updates an excel file, from which data is to be extracted and updated to another excel which will be available in microsoft sharepoint. The file with the required python and excel files is given and are not to be disturbed.
I have created the web page in django and and the button as well. I have also written a python script which extracts the data and updates in the other excel file which is as follows
import pandas as pd
import openpyxl as op
def xl():
A = pd.read_excel('testcase_database.xlsx') #The excel file from which the data is to be taken
B = pd.read_excel('S32K3XX_SecureBAF_Sprint3_Test_Report.xlsx', sheet_name='Test_Report') #The excel file to which the data is to be updated
tcname = A['Unnamed: 2']#Test case nams in A
fcname = B['Unnamed: 5'] #Test case names in B
pi = A['Unnamed: 5'] #Results in A
temp = ""
temp1 = ""
#To compare the test case names in A and B and then printing that test case result in B
for i, temp in enumerate(tcname):
for j, temp1 in enumerate(fcname):
if temp == temp1:
B['Unnamed: 15'].iat[j] = pi[i]
else:
continue
pf = B['Unnamed: 15'].tolist() #Results in B
wb = op.load_workbook('S32K3XX_SecureBAF_Sprint3_Test_Report.xlsx')
ws=wb.active
for i in range(1,512):
c = ws.cell(row=i,column=16)
j = i-1
c.value = pf[j]
wb.save('Sprint3_Test_Report.xlsx')
return xl
But i am having problem with running the python script in the cmd when the button is clicked , and not sure on how to deploy it in a web page which can be accesed by anyone, and where do I host the folder containing the python and excel files for the webpage to access and update them, and how to make them accessible via sharepoint?
To run the script, make it a django view:
from django.http import HttpResponse
from openpyxl import Workbook
def export_excel(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = f'attachment; filename="filename.xls"'
wb = Workbook()
...
wb.save(response)
return response
The function above makes your browser download the generated excel file.
I don't know about the microsoft sharepoint part, but that's a start.
Related
I am trying to run a query, with the result saved as a CSV that is uploaded to a SharePoint folder. This is within Databricks via Pyspark.
My code below is close to doing this, but the final line is not functioning correctly - the file generated in SharePoint does not contain any data, though the dataframe does.
I'm new to Python and Databricks, if anyone can provide some guidance on how to correct that final line I'd really appreciate it!
from shareplum import Site
from shareplum.site import Version
import pandas as pd
sharepointUsername =
sharepointPassword =
sharepointSite =
website =
sharepointFolder =
# Connect to SharePoint Folder
authcookie = Office365(website, username=sharepointUsername, password=sharepointPassword).GetCookies()
site = Site(sharepointSite, version=Version.v2016, authcookie=authcookie)
folder = site.Folder(sharepointFolder)
FileName = "Data_Export.csv"
Query = "SELECT * FROM TABLE"
df = spark.sql(Query)
pandasdf = df.toPandas()
folder.upload_file(pandasdf.to_csv(FileName, encoding = 'utf-8'), FileName)
Sure my code is still garbage, but it does work. I needed to convert the dataframe into a variable containing CSV formatted data prior to uploading it to SharePoint; effectively I was trying to skip a step before. Last two lines were updated:
from shareplum.site import Version
import pandas as pd
sharepointUsername =
sharepointPassword =
sharepointSite =
website =
sharepointFolder =
# Connect to SharePoint Folder
authcookie = Office365(website, username=sharepointUsername, password=sharepointPassword).GetCookies()
site = Site(sharepointSite, version=Version.v2016, authcookie=authcookie)
folder = site.Folder(sharepointFolder)
FileName = "Data_Export.csv"
Query = "SELECT * FROM TABLE"
df = (spark.sql(QueryAllocation)).toPandas().to_csv(header=True, index=False, encoding='utf-8')
folder.upload_file(df, FileName)
I am trying to run a VBA Macro in an xlsm workbook using python 3.7 in Spyder. This workbook has two worksheets.
The code that I have currently runs and saves the new file with no problems, however it is not triggering the VBA like it should.
I know this macro works because if I manually click the button in Excel, it works just fine.
Could someone assist with this? I checked the Macro Settings under the Trust Center and all macros are enabled so I do not think it is a permissions issue, however I am not an admin on this pc.
The code is below:
import os
import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
wb = xl.Workbooks.Open("Z:\FolderName\FolderName2\FileName.xlsm")
xl.Application.Run("MacroName")
wb.SaveAs("Z:\FolderName\FolderName2\FileName1.xlsm")
wb.Close()
xl.Quit()
This can be done easily through xlwings. Once I switched to that library then I was able to quickly get this script working.
First make sure you have your All.xlsm file in your current working or in your User/Documents(Sometimes it working from yourDocuments directory and sometimes not, so better to have in both)
pass your macro name along with the file name that contains the macro you can make change to Parameters like ReadOnly or Savechanges according to your requirement
And be make sure to deleta xl object after each run
import win32com.client
xl =win32com.client.dynamic.Dispatch('Excel.Application')
xl.Workbooks.Open(Filename = XYZ.xls, ReadOnly= 1)
xl.Application.Run('All.xlsm!yourmacroname')
xl.Workbooks(1).Close(SaveChanges=1)
xl.Application.Quit()
del xl
Running Excel Macro from Python
To Run a Excel Marcro from python, You don't need almost nothing. Below a script that does the job. The advantage of Updating data from a macro inside Excel is that you immediatly see the result. You don't have to save or close the workbook first. I use this methode to update real-time stock quotes. It is fast and stable.
This is just an example, but you can do anything with macros inside Excel.
from os import system, path
import win32com.client as win32
from time import sleep
def isWorkbookOpen(xlPath, xlFileName):
SeachXl = xlPath + "~$" + xlFileName
if path.exists(SeachXl):
return True
else:
return False
def xlRunMacro(macroLink):
PathFile = macroLink[0]
xlMacro = macroLink[1]
isLinkReady = False
# Create the link with the open existing workbook
win32.pythoncom.CoInitialize()
xl = win32.Dispatch("Excel.Application")
try:
wb = win32.GetObject(PathFile)
isLinkReady = True
except:
NoteToAdd = 'Can not create the link with ' + PathFile
print(NoteToAdd)
if isLinkReady:
# If the link with the workbook exist, then run the Excel macro
try:
xl.Application.Run(xlMacro)
except:
NoteToAdd = 'Running Excel Macro ' + xlMacro + ' failed !!!'
print(NoteToAdd)
del xl
def mainProgam(macroSettings):
FullMacroLink = []
PathFile = macroSettings[0] + macroSettings[1]
FullMacroLink.append(PathFile)
FullModuleSubrout = macroSettings[1] + '!' + macroSettings[2] + '.' + macroSettings[3]
FullMacroLink.append(FullModuleSubrout)
if isWorkbookOpen(macroSettings[0], macroSettings[1]) == False:
# If the workbook is not open, Open workbook first.
system(f'start excel.exe "{PathFile}"')
# Give some time to start up Excel
sleep(2)
xlRunMacro(FullMacroLink)
def main():
macroSettings = []
# The settings below will execute the macro example
xlPath = r'C:\test\\' # Change add your needs
macroSettings.append(xlPath)
workbookName = 'Example.xlsm' # Change add your needs
macroSettings.append(workbookName)
xlModule = "Updates" # Change add your needs
macroSettings.append(xlModule)
xlSubroutine = "UpdateCurrentTime" # Change add your needs
macroSettings.append(xlSubroutine)
mainProgam(macroSettings)
if __name__ == "__main__":
main()
exit()
VBA Excel Macro
Option Explicit
Sub UpdateCurrentTime()
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Current-Time")
With sht
sht.Cells(2, 1).Value = Format(Now(), "hh:mm:ss")
End With
End Sub
You can use it also as a dynamic module too. Save the module above as RunExcelMacro.py in Your python project. After just use the following lines:
from RunExcelMacro import mainProgam
mainProgram(macroSettings)
It will do the job, succes ...
You need to reference the module name as well
Example here my vba code under Module1
Option Explicit
Public Sub Example()
MsgBox "Hello 0m3r"
End Sub
and here is my python
from win32com.client import Dispatch
def run_excel_macro():
try:
excel = Dispatch("Excel.Application")
excel.Visible = True
workbook = excel.Workbooks.Open(
r"D:\Documents\Book1.xlsm")
workbook.Application.Run("Module1.Example")
workbook.SaveAs(r"D:\Documents\Book5.xlsm")
excel.Quit()
except IOError:
print("Error")
if __name__ == "__main__":
run_excel_macro()
I am trying to print Excel files to pdf with xlwings. I am using the excel api for this.
I have tried it in two ways:
1/ Using the PrintOut() call with PrintToFile argument:
wb.api.PrintOut(PrintToFile=True, PrToFileName="5.pdf", Preview=True)
The problem here is Excel just prints the file, ignoring my additional settings.
2/ Using ExportAsFixedFormat
wb.api.ExportAsFixedFormat(0, str(SwmId) + ".pdf")
Here Excel flashes a bit, but does not do anything in the end.
For the record: I can't use a macro and call it from Python because I have about a thousand of these Excel files. So, I can't put the macro in every single one of them. It would probably be a workaround to create a custom function in VBA and than call it every file. But, honestly, it would be easier if I could just do this directly from Python, in one line of code.
Below is a self-standing code example of what worked on my machine to print an excel workbook to pdf (using the ExportAsFixedFormat method):
# Environment
# -----------
# OS: Windows 10
# Excel: 2013
# python: 3.7.4
# xlwings: 0.15.8
import os
import xlwings as xw
# Initialize new excel workbook
book = xw.Book()
sheet = book.sheets[0]
sheet.range("A1").value = "dolphins"
# Construct path for pdf file
current_work_dir = os.getcwd()
pdf_path = os.path.join(current_work_dir, "workbook_printout.pdf")
# Save excel workbook to pdf file
print(f"Saving workbook as '{pdf_path}' ...")
book.api.ExportAsFixedFormat(0, pdf_path)
# Open the created pdf file
print(f"Opening pdf file with default application ...")
os.startfile(pdf_path)
xlwings documentation recommends using xw.App():
from pathlib import Path
import xlwings as xw
import os
with xw.App() as app:
# user will not even see the excel opening up
app.visible = False
book = app.books.open(path_to_excelfile)
sheet = book.sheets[0]
sheet.page_setup.print_area = '$A$1:$Q$66'
sheet.range("A1").value = "experimental"
# Construct path for pdf file
current_work_dir = os.getcwd()
pdf_file_name = "pdf_workbook_printout.pdf"
pdf_path = Path(current_work_dir, pdf_file_name)
# Save excel workbook as pdf and showing it
sheet.to_pdf(path=pdf_path, show=True)
I need to write a code to tell Python to look into an excel sheet, extract a table and paste it as an image on an email. Thus far, I have managed to tell Python to write web content onto that excel file from a website and even send out an email. The gap is really lies in the step described in the first sentence above. The whole idea of this project is really not needing to open the excel file at all and running everything via Python.
Been spending quite abit of time on this but the best result so far is only one where Python can print cell data from excel but no the entire table as an image.
Really appreciate some help here. Thank you and cheers.
Code attached:
import requests
response = requests.get("")
txt=response.text
lines = txt.split('\n')
print(lines[25])
from openpyxl import load_workbook
wb = load_workbook(filename = 'abc.xlsm', read_only=False, keep_vba=True)
ws1 = wb['Sheet1']
ws1['A2'].value = (lines[25])
wb.save('abc.xlsm')
# Up to this point I have managed to get Python to extract web content and write into an existing excel file.
import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "My Subject"
newMail.Body = "My Body"
newMail.To = "..."
newMail.Send()
#This sends out the email. This tries to get Python to print out the entire sheet but it comes out quite jumbled:
import xlrd
book = xlrd.open_workbook('C:/Users/adriel.cheng/Desktop/Inspection Sales_v8.xlsm')
print (book.nsheets)
print (book.sheet_names())
#An alternative approac but it also comes out jumbled:
import pandas as pd
xl = pd.ExcelFile('C:/Users/adriel.cheng/Desktop/Inspection Sales_v8.xlsm')
xl.sheet_names
df = xl.parse("Inspections sheet")
print (df)
I'm using formulas:
wsheet.write(i, j, Formula('HYPERLINK(%s;"Link")' % click), docnm)
in my Excel files
and when it first opens up it goes into "Protected View". My formulas don't load until after I click "Enable Editing". Is there anyway to get my numbers to show up even if Protected Mode is on?
I found a similar topic on this link Protected View in Microsoft Excel 2010 and Python , but there aren't any useful answers..
Can someone help me please?
Complete code:
from xlwt import easyxf,Formula
import xlwt
wbook = xlwt.Workbook()
wsheet = wbook.add_sheet("MySheet")
wsheet.col(j).width = 17000
link="https://stackoverflow.com/"
click="http://ccwebviewer.ac.de.eu.ericsson.se/~"+excelbranch+link
click='"'+str(click)+'"'
linkName='"'+"LINK"+'"'
wsheet.write(1, 1, Formula('HYPERLINK(%s;%s)' % (click,linkName)))
wbook.save("excel.xls")
I have tried a basic example with the code below and seem to be able to open the workbook produced without a prompt and follow the link:
import xlwt
link_url = 'http://stackoverflow.com/questions/21430921/disable-protected-view-mode-in-excel-files-with-xlwt-python' #'file1.csv'
outputfile = 'outputList.xls'
wbk = xlwt.Workbook()
wsheet = wbk.add_sheet('sheet 1')
xlformula = 'HYPERLINK("'+link_url+'", "Link")'
wsheet.write(0, 0, xlwt.ExcelFormula.Formula(xlformula))
wbk.save(outputfile)
This basically creates a new workbook and writes a link to the URL for this question and saves the workbook.