Chaque semaine, découvrez de nouvelles astuces et bonnes pratiques pour devenir un expert !
Voir l'index06-14-2019 03:19 AM - modifié 11-26-2019 02:14 AM
How To: Produire un document réglementaire à partir d'Alteryx, Python et d'un modèle
Prerequisites
Procédure
# List all non-standard packages to be imported by your
# script here (only missing packages will be installed)
from ayx import Package
Package.installPackages(['openpyxl'])
#! Python 3
# - Copy and Paste Ranges using OpenPyXl library
import openpyxl
import os
from ayx import Alteryx
#Get Alteryx working directory
dir= Alteryx.getWorkflowConstant("Engine.WorkflowDirectory")
#Prepare the spreadsheets to copy from and paste too.
#File to be copied
wb = openpyxl.load_workbook(dir + "Data\\TempData.xlsx") #Add file name
sheet = wb["Sheet1"] #Add Sheet name
#File to be pasted into
template = openpyxl.load_workbook(dir + "Template\\Template_liability_data_reporting_2019_v2.7.1_ détail.xlsx") #Add file name
temp_sheet = template["T01.00"] #Add Sheet name
#Copy range of cells as a nested list
#Takes: start cell, end cell, and sheet you want to copy from.
def copyRange(startCol, startRow, endCol, endRow, sheet):
rangeSelected = []
#Loops through selected Rows
for i in range(startRow,endRow + 1,1):
#Appends the row to a RowSelected list
rowSelected = []
for j in range(startCol,endCol+1,1):
rowSelected.append(sheet.cell(row = i, column = j).value)
#Adds the RowSelected List and nests inside the rangeSelected
rangeSelected.append(rowSelected)
return rangeSelected
#Paste range
#Paste data from copyRange into template sheet
def pasteRange(startCol, startRow, endCol, endRow, sheetReceiving,copiedData):
countRow = 0
for i in range(startRow,endRow+1,1):
countCol = 0
for j in range(startCol,endCol+1,1):
sheetReceiving.cell(row = i, column = j).value = copiedData[countRow][countCol]
countCol += 1
countRow += 1
def createData():
print("Processing...")
#Dans le cas du LDR, on effectue l'extraction de la matrice du fichier temporaire
selectedRange = copyRange(1,1,24,70,sheet) #Change the 4 number values
#On l'injecte ensuite au bon endroit (num colonne début,num ligne début,num colonne fin, num ligne fin )
#Il serait aussi possible de récupérer dynamiquement ces valeurs
pastingRange = pasteRange(3,5,26,74,temp_sheet,selectedRange) #Change the 4 number values
#You can save the template as another file to create a new file here too.s
template.save(dir + "Livrable\\LDR_Filled.xlsx")
print("Range copied and pasted!")
Workflow complet
Problèmes potentiels
Resources additionnelles