To read data from a SQL database using Python, you can use the pyodbc library to connect to the database and execute a SQL query to select the data. Here’s a sample code import pyodbc # Connect to the SQL database conn = pyodbc.connect('Driver={SQL Server};Server=server_name;Database=database_name;Trusted_Connection=yes;') # Define the SQL query to select data from the database sql_query = "SELECT * FROM table_name" # Execute the SQL query and fetch the results cursor = conn.cursor() cursor.execute(sql_query) rows = cursor.fetchall() # Iterate through the rows and print the data for row in rows: print(row) # Close the database connection conn.close() In this example, the SQL query SELECT * FROM table_name selects all the rows from the table table_name in the SQL database. You can modify the SQL query to match the columns and table structure in your database. Once the query is executed, the fetchall() method retrieves all the rows from the cursor, and the rows can be iterated through to access the individual values.
Write data to SQL database using Python
To write data to a SQL database using Python, you can use the pyodbc library to connect to the database and insert the data. Here’s a sample code. import pyodbc # Connect to the SQL database conn = pyodbc.connect('Driver={SQL Server};Server=server_name;Database=database_name;Trusted_Connection=yes;') # Define the data to insert into the database data = [ ('John', 'Doe', 25), ('Jane', 'Doe', 30), ('Bob', 'Smith', 40), ] # Iterate through the data and insert it into the database for row in data: cursor = conn.cursor() cursor.execute("INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)", row[0], row[1], row[2]) cursor.commit() # Close the database connection conn.close() In this example, the data is defined as a list of tuples, where each tuple represents a row of data to be inserted into the table table_name in the SQL database. You can modify the SQL query in the cursor.execute() method to match the columns and table structure in your database.
Load data from Excel to Progress database using Python
To write data to a Progress database from an Excel file using Python, you can use the openpyxl library to read the data from the Excel file and the pyodbc library to connect to the Progress database and insert the data. Here’s a sample code: import pyodbc from openpyxl import load_workbook # Connect to the Progress database conn = pyodbc.connect('Driver={Progress OpenEdge 10.2B driver};HOST=hostname;PORT=port;DATABASE=database_name;UID=username;PWD=password') # Load the Excel workbook workbook = load_workbook(filename='data.xlsx') # Select the worksheet worksheet = workbook['Sheet1'] # Iterate through each row in the worksheet and insert the data into the database for row in worksheet.iter_rows(values_only=True): cursor = conn.cursor() cursor.execute("INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)", row[0], row[1], row[2]) cursor.commit() # Close the database connection conn.close() In this example, the Excel file data.xlsx has data in the first three columns of Sheet1, and the data is inserted into a table named table_name in the Progress database. You can modify the SQL query in the cursor.execute() method to match the columns and table structure in your database.
Load data from CSV to Progress database using Python
Here’s a sample Python code to write data from a CSV file to a Progress database using the pyodbc library import pyodbc import csv # Set up the connection to the Progress database conn = pyodbc.connect('DRIVER={Progress OpenEdge 10.2A Driver};HOST=<your host>;PORT=<port number>;DB=<database name>;UID=<username>;PWD=<password>') # Set up a cursor to execute SQL queries cursor = conn.cursor() # Open the CSV file and read the data with open('data.csv', 'r') as file: csv_data = csv.reader(file) # Define the SQL query to insert data into a table sql_query = "INSERT INTO my_table (column1, column2, column3) VALUES (?, ?, ?)" # Loop through each row of data in the CSV file and insert it into the database for row in csv_data: cursor.execute(sql_query, (row[0], row[1], row[2])) # Commit the changes to the database and close the connection conn.commit() conn.close() In this example, we first set up a connection to the Progress database using the pyodbc library. We then define a cursor to execute SQL queries, and open the CSV file using the csv library. We then define an SQL query to insert data into a table, and loop through each row of data in the CSV file. For each row, we execute the SQL query using the cursor and pass in the values from the row as parameters. Finally, we commit the changes to the database using the commit() method and close the connection.
Read data to Progress database using Python
Here’s a sample Python code to read data from a Progress database using the pyodbc library import pyodbc # Set up the connection to the Progress database conn = pyodbc.connect('DRIVER={Progress OpenEdge 10.2A Driver};HOST=<your host>;PORT=<port number>;DB=<database name>;UID=<username>;PWD=<password>') # Set up a cursor to execute SQL queries cursor = conn.cursor() # Define the SQL query to select data from a table sql_query = "SELECT column1, column2, column3 FROM my_table" # Execute the SQL query and fetch the results cursor.execute(sql_query) results = cursor.fetchall() # Print the results for row in results: print(row) # Close the connection conn.close() In this example, we first set up a connection to the Progress database using the pyodbc library. We then define a cursor to execute SQL queries, and define an SQL query to select data from a table. We then execute the SQL query using the cursor and fetch the results using the fetchall() method. Finally, we loop through the results and print each row, and close the connection.
Write data to Progress database using Python
Here’s a sample code in Python to write data to a Progress database using the pyodbc library import pyodbc # Set up the connection to the Progress database conn = pyodbc.connect('DRIVER={Progress OpenEdge 10.2A Driver};HOST=<your host>;PORT=<port number>;DB=<database name>;UID=<username>;PWD=<password>') # Set up a cursor to execute SQL queries cursor = conn.cursor() # Define the SQL query to insert data into a table sql_query = "INSERT INTO my_table (column1, column2, column3) VALUES (?, ?, ?)" # Define the data to be inserted data = [ ('value1', 'value2', 'value3'), ('value4', 'value5', 'value6'), ('value7', 'value8', 'value9') ] # Execute the SQL query for each row of data for row in data: cursor.execute(sql_query, row) # Commit the changes to the database conn.commit() # Close the connection conn.close() In this example, we first set up a connection to the Progress database using the pyodbc library. We then define a cursor to execute SQL queries, and define an SQL query to insert data into a table. We also define the data to be inserted as a list of tuples. We then loop through the data and execute the SQL query for each row using the cursor. Finally, we commit the changes to the database and close the connection.
.NET code to extract attachments from Outlook
Here’s an example code in C# to extract attachments from Outlook using the Microsoft Graph API using Microsoft.Graph; using Microsoft.Graph.Auth; using Microsoft.Identity.Client; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; // Define the authentication settings for the Microsoft Graph API string clientId = "your_client_id_here"; string tenantId = "your_tenant_id_here"; string clientSecret = "your_client_secret_here"; string[] scopes = { "https://graph.microsoft.com/.default" }; IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(clientId) .WithTenantId(tenantId) .WithClientSecret(clientSecret) .Build(); ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication, scopes); // Define the ID of the Outlook email to extract attachments from string messageId = "your_message_id_here"; // Create an instance of the GraphServiceClient class to interact with the Microsoft Graph API GraphServiceClient graphClient = new GraphServiceClient(authProvider); // Define a list to store the extracted attachments List<Attachment> attachments = new List<Attachment>(); // Use the GraphServiceClient to retrieve the email message with the specified ID, including its attachments Message message = await graphClient.Me.Messages[messageId].Request().Expand("attachments").GetAsync(); // Loop through each attachment in the email message foreach (Attachment attachment in message.Attachments) { // Check if the attachment is a file attachment if (attachment is FileAttachment fileAttachment) { // Download the file to a byte array byte[] fileContents = await graphClient.Me.Messages[messageId].Attachments[fileAttachment.Id].Content.Request().GetAsync(); // Define the path and filename to save the attachment to string attachmentPath = "C:\attachments\" + fileAttachment.Name; // Save the file to disk File.WriteAllBytes(attachmentPath, fileContents); // Add the attachment to the list of extracted attachments attachments.Add(fileAttachment); } } // Display the list of extracted attachments foreach (Attachment attachment in attachments) { Console.WriteLine("Attachment Name: " + attachment.Name); } This code uses the Microsoft Graph API to retrieve the specified email message from Outlook, including its attachments. It then loops through each attachment and uses the Content.Request().GetAsync() method to download the contents of the attachment as a byte array. The code then uses the File.WriteAllBytes() method to save the attachment to disk, and adds the attachment to a list of extracted attachments. Finally, the code displays the list of extracted attachments using the Console.WriteLine() method. Note that you will need to replace the placeholders in the code (e.g. your_client_id_here, your_tenant_id_here, your_client_secret_here, and your_message_id_here) with your own values.
.NET code to extract data from web page
Here’s an example code in C# to extract data from a Wikipedia web page using the HtmlAgilityPack library using HtmlAgilityPack; using System; using System.Net; // Define the URL of the Wikipedia page string url = "https://en.wikipedia.org/wiki/Artificial_intelligence"; // Create an instance of the WebClient class to download the page WebClient webClient = new WebClient(); string htmlString = webClient.DownloadString(url); // Create an instance of the HtmlDocument class to parse the HTML HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlString); // Define a string to store the extracted text string extractedText = ""; // Loop through each paragraph element in the HTML document foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//p")) { // Extract the text from the current paragraph extractedText += node.InnerText; } // Display the extracted text Console.WriteLine(extractedText); This code uses the WebClient class to download the HTML content of the Wikipedia page, and the HtmlDocument class from the HtmlAgilityPack library to parse the HTML. The code then loops through each paragraph element (<p>) in the HTML document and uses the InnerText property to extract the text from each paragraph. The extracted text is stored in a string variable and then displayed using the Console.WriteLine() method.
.NET code to extract data from PDF file
Here’s an example code in C# to extract data from a PDF file using the iTextSharp library using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; using System.IO; // Define the path of the PDF file string filePath = "path/to/pdf/file.pdf"; // Create an instance of the PdfReader class to read the PDF file PdfReader pdfReader = new PdfReader(filePath); // Define a string to store the extracted text string extractedText = ""; // Loop through each page of the PDF file for (int i = 1; i <= pdfReader.NumberOfPages; i++) { // Extract text from the current page extractedText += PdfTextExtractor.GetTextFromPage(pdfReader, i); } // Close the PdfReader object pdfReader.Close(); // Write the extracted text to a text file File.WriteAllText("path/to/output/file.txt", extractedText); // Display a message indicating that the extraction is complete Console.WriteLine("Extraction complete."); This code uses the PdfReader class to read the PDF file, loops through each page of the file, and uses the PdfTextExtractor class to extract the text from each page. The extracted text is stored in a string variable and then written to a text file using the File.WriteAllText() method. Finally, a message is displayed to indicate that the extraction is complete.
Write data to word document using Python
Here’s an example code in Python to write data to an Access Word document using the docx library import docx # Open the Word document doc = docx.Document('path/to/document.docx') # Define the data to be written data = [ ['Name', 'Age', 'Gender'], ['John', '25', 'Male'], ['Sarah', '30', 'Female'], ['David', '22', 'Male'] ] # Add a table to the document table = doc.add_table(rows=1, cols=3) table.style = 'Table Grid' # Add the header row to the table hdr_cells = table.rows[0].cells for i in range(3): hdr_cells[i].text = data[0][i] # Add the data rows to the table for row in data[1:]: row_cells = table.add_row().cells for i in range(3): row_cells[i].text = row[i] # Save the changes to the document doc.save('path/to/document.docx') print('Data has been written to the Word document successfully.') This code opens a Word document using the Document() method, defines a list of data to be written to the document, adds a table to the document using the add_table() method, adds the header row to the table using the first row of the data list, and adds the data rows to the table using the remaining rows of the data list. Finally, it saves the changes to the document using the save() method and prints a message to indicate that the data has been written successfully.