Restoring a SQL Server database backup file (.bak)

Introduction On regular basis we need to restore the database, either will only schema of the database, or many times both data as well as schema.  Most of the applications (either Web application e.g. ASP.NET based apps, or Windows applications e.g. WPF based apps) requires 'reference data' such as drop-down data, types of users, etc. So what is the most optimum, reliable way of moving database either on separate database server, or on the same database server but with different database name? There are several ways to achieve this objective but my personal preference is to first create the database backup file i.e. 'my_backup.bak' using Microsoft SQL Server Management Studio , and then restoring that backup file to create new database.  This article focuses on restoring the backup file to create a new database. How to create the backup file will be covered in another article.   This article addresses below points: 1.   How to check the name and path of th...

Reading Excel in Python - Cheatsheet


Introduction

Using xlwings and pandas we will learn how to open, manipulate and save the MS Excel file in Python. This is a reference material for experienced programmers. 

Working with Microsoft Excel Files

►  Import the modules required to work with Excel file. 
  import xlwings as xw
  import pandas as pd
  from collections import defaultdict 
►  Specify the Excel file name (and not the path).

►  Get the path of the directory separately in a different variable. 

►  Alternatively you can also specify the full path and pass it directly to the Book method. 

  excelName = "MyExcel.xlsx"
  workingDir = "C:\MyApp\MyFolder" # no slash in the end
  # Join method of os module can be used to join the paths and create a full path
  # And read the whole Excel file and the created object is 'reportExcel'
  reportExcel = xw.Book(os.path.join(workingDir, excelName))
  app = xw.apps.active # it seems this opens the Excel file 

Make the necessary changes to the file 

►  Add a single value to a specific Cell using its row and column position
  row_index = 1  # Second row in the Excel, first row starts with zero
  col_index = 1  # First column of the Excel
  reportExcel.sheets('Results').range(row_index, col_index).value = "12.12"
►  Add a collection of rows and columns (i.e. a table) to the Excel sheet
  # Assume you have a Employee DataFrame named - df 
  # Below line will add this DataFrame to the Excel starting from the E1 Cell
  reportExcel.sheets('Connection Report').range('e1').value = df
►  Add a column to the Excel sheet
# All the Employee names will be added to the column B2 of 'Report' Excel sheet - assume that the emp_names is a pandas Series
reportExcel.sheets('Report').range('b2').options(transpose=True).value = emp_names 

Finally save and close the Excel file

reportExcel.save(path=workingDir + r'MyFile.xlsx')
app.quit()  # this will close the opened Excel file 
sys.exit(0)

Summary

This post provides a quick reference to manipulate Microsoft Excel file in Python. 


Your feedback

Your feedback is of paramount importance to us, provide your valuable inputs; what else you would like to read about, or if you would like the author to elaborate on any specific topic from this article, or any other article on this blog.

Disclaimer

The material presented in this blog is to help educate the developers, and the author(s) & owner(s) of this blog are not responsible of any damage arising out of use of the code snippets or any other contents of this blog.

Comments