Select Page

This is the scrapbook.

Here is where random information goes. Most of them are little snippets of information, stories, quick fixes or references to additional scrapbook-style information – short notes that were helpful to me and might be useful to someone else. Of course, this is only meant as a suggestion, without any guarantee or assurance of function or feasibility. If you would like my professional, technical support, please contact me at https://c-7.de.

Die “Kladde”

Hier landen zufällige Informationen. Die meisten davon sind kleine Informationsschnipsel,Geschichten,  schnelle Lösungen oder Verweise auf zusätzliche Informationen im Sammelalbum-Stil – kurze Notizen, die für mich hilfreich waren und vielleicht auch für jemand anderen nützlich sein können. Natürlich ist das hier nur als Anregung zu verstehen, ohne jegliche Gewähr oder Zusicherung einer Funktion oder Machbarkeit. Wenn Sie meine professionelle, technische Unterstützung möchten, Kontaktieren Sie mich bitte über https://c-7.de

Excel upload to OpenProject

by | Oct 14, 2023 | Tech Corner

Python script to upload Excel data to OpenProject (not fully tested)
import pandas as pd
import requests
import argparse
“””
This Python script is designed to facilitate the bulk uploading of work packages into an OpenProject instance. Work packages, which are project management entities, can be efficiently imported from an Excel file to OpenProject using this tool.
There are 2 header rows, one from the original Excel table and the correspondences for OpenProject.
Excel sample:
Header1 OpenProject Mapping:       subject                                                        description
Header2 Excel Header:                    Leistungs- und Liefergegenstand               Beschreibung
                                                          Server                                                        Der Server soll ….
                                                           Heizung und Kühlung                                 Die Klimaanlage ist …
The script accepts user-provided parameters such as the OpenProject domain, API key for authentication, the target work package ID, and the path to the input Excel file containing the work package data. It then processes the Excel file, converting the data into JSON format, and sends HTTP POST requests to the OpenProject server to create or update the work packages.
Each row in the Excel file corresponds to a work package. Customizable fields and fixed values are assigned to the work packages, and the ‘subject’ field can be formatted as Markdown with an HTML preview for content rendering.
Upon completion, the script provides feedback on the status of each request, indicating whether the data was successfully uploaded or any encountered errors. This tool aims to streamline the process of populating and managing work packages in an OpenProject instance.
German:
Dieses Python-Skript wurde entwickelt, um das Massenhochladen von Arbeitspaketen in eine OpenProject-Instanz zu erleichtern. Arbeitspakete, die Projektmanagementeinheiten sind, können effizient aus einer Excel-Datei in OpenProject mithilfe dieses Tools importiert werden.
Es gibt 2 Kopfzeilen, die von dem Original Excel Tabelle (2) und die Entsprechungen für OpenProject (1)
Excel Beispiel:
Header1 OpenProject Mapping:       subject                                                        description
Header2 Excel Header:                    Leistungs- und Liefergegenstand               Beschreibung
AP 1                                                Server                                                          Der Server soll ….
AP 2                                                 Heizung und Kühlung                                 Die Klimaanlage ist …
Das Skript akzeptiert vom Benutzer bereitgestellte Parameter wie die OpenProject-Domäne, den API-Schlüssel zur Authentifizierung, die Ziel-Arbeitspaket-ID und den Pfad zur Eingabe-Excel-Datei mit den Arbeitspaketdaten. Es verarbeitet dann die Excel-Datei, konvertiert die Daten in das JSON-Format und sendet HTTP-POST-Anfragen an den OpenProject-Server, um die Arbeitspakete zu erstellen oder zu aktualisieren.
Jede Zeile in der Excel-Datei entspricht einem Arbeitspaket. Anpassbare Felder und feste Werte werden den Arbeitspaketen zugewiesen, und das Feld ‘subject’ kann als Markdown mit einer HTML-Vorschau für die Inhaltsdarstellung formatiert werden.
Nach Abschluss gibt das Skript Feedback zum Status jeder Anfrage und zeigt an, ob die Daten erfolgreich hochgeladen wurden oder Fehler aufgetreten sind. Dieses Tool soll den Prozess des Befüllens und Verwaltens von Arbeitspaketen in einer OpenProject-Instanz optimieren.
Author: Claus Siebeneicher
Date: Oct. 2023
“””
def main():
parser = argparse.ArgumentParser(description=”Process an Excel file and send requests to a server.”)
parser.add_argument(“–domain”, required=True, help=”Your domain”)
parser.add_argument(“–api-key”, required=True, help=”Your API key”)
parser.add_argument(“–project-id”, required=True, help=”Your project ID”)
parser.add_argument(“–input-file”, required=True, help=”Path to the input Excel file”)
args = parser.parse_args()
DOMAIN = args.domain
API_KEY = args.api_key
WP_ID = args.project_id
INFILE = args.input_file
# Define the URL for the requests
url = f”http://{DOMAIN}/api/v3/projects/{WP_ID}/work_packages”
request_headers = {
“Content-Type”: “application/json”,
# “Authorization”: f”apikey {API_KEY}”
}
# Read the Excel file using pandas
try:
df = pd.read_excel(INFILE)
exceptFileNotFoundError:
print(f”File not found: {INFILE}”)
return
# Get the headers from the first row of the Excel file
headers = list(df.columns)
# Define the fixed fields
fixed_fields = {
“scheduleManually”: False,
# “customField2”: “Foo2”,
“readonly”: False,
“startDate”: None,
“dueDate”: None,
“derivedStartDate”: None,
“derivedDueDate”: None
}
# Iterate through each row in the DataFrame and send a request for each row
for _, row in df.iterrows():
data = {headers[i]: str(row[i]) for i in range(len(headers))}
data.update(fixed_fields) # Merge the fixed fields with the row-specific data
# Special handling for the ‘description’ column
if’description’in data:
data[‘description’] = {
“format”: “markdown”,
“raw”: data[‘description’],
“html”: f”<p>{data[‘description’]}</p>”
}
# Create the JSON data for the request
json_data = {header: content for header, content in data.items()}
# Send the POST request to the server
try:
response = requests.post(url, headers=request_headers, json=json_data,auth=(“apikey”, API_KEY) )
#response = requests.post(url, json=payload, headers=headers, auth=(“apikey”, API_KEY))
response.raise_for_status()
print(f”Request successful for data: {json_data}”)
print(f”Request successful – response: {response}”)
# print(f”Request successful”)
except requests.exceptions.RequestException as e:
print(f”Request failed: {str(e)}”)
print(json_data)
print(“Requests completed.”)
if __name__ == “__main__”:
main()