Frag Me Nation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1) Write a Python program to read ZIP files.

import zipfile

with zipfile.ZipFile('example.zip', 'r') as zip:


zip.printdir()

2) Write a Python program to extract files from ZIP files.

import zipfile

# Extract all files from a ZIP

with zipfile.ZipFile('example.zip', 'r') as zip:

zip.extractall('output_directory') # Extract to 'output_directory'

3) Write a Python program to create a ZIP file and add the files to it

import zipfile

# Create and add files to a ZIP

with zipfile.ZipFile('new_archive.zip', 'w') as zip:

zip.write('file1.txt') # Add file1.txt

zip.write('file2.txt') # Add file2.txt

4) Write a Python program using shutil library functions to: Copying Files and

Folders ii) Moving and Renaming Files and Folders

import shutil

# Copy files and folders

shutil.copy('source.txt', 'destination_folder/') # Copy file

shutil.copytree('source_folder', 'destination_folder') # Copy folder

ii)
# Move a file or folder

shutil.move("source.txt", "new_location.txt")

# Rename a file

shutil.move("old_name.txt", "new_name.txt")

5) Explain the process of getting sheets from the workbook with suitable

example.

from openpyxl import load_workbook

# Load the workbook

wb = load_workbook("example.xlsx")

# Get sheet names

print("Sheets:", wb.sheetnames)

# Access a specific sheet

sheet = wb["Sheet1"]

print("Active Sheet:", sheet.title)

Explanation:

• Use load_workbook to load an Excel file.


• Retrieve all sheet names using wb.sheetnames.
• Access a specific sheet using the sheet name.

6) Explain the process of reading cell values from the sheets with suitable examples

from openpyxl import load_workbook

# Load workbook and sheet

wb = load_workbook("example.xlsx")
sheet = wb["Sheet1"]

# Read cell values

print("Cell A1:", sheet["A1"].value)

print("Cell B2:", sheet.cell(row=2, column=2).value)

Explanation:

• Use sheet["CellName"] or sheet.cell(row, column) to access specific cell


values.

7) Write a Python program to create sheets in Excel spreadsheet


from openpyxl import Workbook

# Create workbook and add sheets


wb = Workbook()
wb.create_sheet(title="NewSheet")
wb.save("created_sheets.xlsx")
print("Sheets created and saved in 'created_sheets.xlsx'")

8) . Write a Python program to remove sheets in Excel spreadsheet.


from openpyxl import Workbook

# Create workbook and remove a sheet


wb = Workbook()
sheet_to_remove = wb.active
wb.remove(sheet_to_remove)
wb.save("updated_sheets.xlsx")
print("Sheet removed and saved in 'updated_sheets.xlsx'")

9) Write a Python program to create an excel file with the name demo.xlsx, write

the statement ‘Hello World’ in the cell B3, change the font style to ‘Cambria’,

size to 20, color to ‘Red’ andmake the statement bold and underline.

from openpyxl import Workbook

from openpyxl.styles import Font

wb = Workbook()

sheet = wb.active
# Write to cell B3

cell = sheet["B3"]

cell.value = "Hello World"

# Apply font style

cell.font = Font(name="Cambria", size=20, color="FF0000", bold=True,


underline="single")

wb.save("demo.xlsx")

print("File 'demo.xlsx' created with styled text.")

10) Write a Python program to demonstrate =sum () formula in Excel

spreadsheet.

from openpyxl import Workbook

wb = Workbook()

sheet = wb.active

# Add numbers and SUM formula

sheet["A1"] = 10

sheet["A2"] = 20

sheet["A3"] = "=SUM(A1:A2)"

wb.save("sum_demo.xlsx")

print("File 'sum_demo.xlsx' created with SUM formula.")


11) Write a Python program to demonstrate =average () formula in Excel

spreadsheet

from openpyxl import Workbook

wb = Workbook()

sheet = wb.active

# Add numbers and AVERAGE formula

sheet["B1"] = 10

sheet["B2"] = 20

sheet["B3"] = "=AVERAGE(B1:B2)"

wb.save("average_demo.xlsx")

print("File 'average_demo.xlsx' created with AVERAGE formula.")

12) Write a Python program to set Row Height and Column Width in Excel

Spreadsheet.

from openpyxl import Workbook

wb = Workbook()

sheet = wb.active

# Set row height and column width

sheet.row_dimensions[1].height = 30

sheet.column_dimensions["A"].width = 20
wb.save("dimensions_demo.xlsx")

print("File 'dimensions_demo.xlsx' created with adjusted dimensions.")

13) Write a Python program to demonstrate merging and unmerging cells in Excel

spreadsheet.

from openpyxl import Workbook

wb = Workbook()

sheet = wb.active

# Merge cells

sheet.merge_cells("A1:C3")

sheet["A1"] = "Merged Cells"

# Unmerge cells

sheet.unmerge_cells("A1:C3")

wb.save("merge_demo.xlsx")

print("File 'merge_demo.xlsx' created with merged and unmerged cells.")

14) Write a Python program to create a Barchart in Excel Spreadsheet

from openpyxl import Workbook

from openpyxl.chart import BarChart, Reference

wb = Workbook()

sheet = wb.active

# Add data
data = [("Category", "Value"), ("A", 10), ("B", 20), ("C", 30)]

for row in data:

sheet.append(row)

# Create chart

chart = BarChart()

data_ref = Reference(sheet, min_col=2, min_row=1, max_col=2, max_row=4)

chart.add_data(data_ref, titles_from_data=True)

sheet.add_chart(chart, "E5")

wb.save("chart_demo.xlsx")

print("File 'chart_demo.xlsx' created with bar chart.")

15) How can you retrieve a tuple of all the Cell Objects (Getting Rows and

Columns from the Sheet) from A1 to F1?

from openpyxl import load_workbook

wb = load_workbook("example.xlsx")

sheet = wb.active

# Get cells from A1 to F1

cells = tuple(sheet["A1:F1"])

print("Cells in A1:F1:", cells)

You might also like