Advertisement
mayankjoin3

openpyxl

Oct 7th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.69 KB | None | 0 0
  1. ### Python `openpyxl` Tutorial for Graduate Students with 20 Examples
  2.  
  3. **Overview:**
  4. The `openpyxl` module is a Python library for reading and writing Excel files (`.xlsx`). It allows you to automate the manipulation of Excel workbooks, such as reading data, creating sheets, formatting cells, and more. This tutorial will guide you through 20 examples, using real-life scenarios involving Indian names and use cases such as managing employee records, exam scores, and financial data.
  5.  
  6. ### Installation:
  7.  
  8. To use `openpyxl`, you need to install it first:
  9.  
  10. ```bash
  11. pip install openpyxl
  12. ```
  13.  
  14. ### Example Dataset (Real-Life Use Case):
  15. Imagine you are managing data for a small business in India. You have an Excel file with employee information such as **Name**, **Age**, **Position**, **Salary**, and **Date of Joining**.
  16.  
  17. **Excel File (`employees.xlsx`):**
  18.  
  19. | Name        | Age | Position      | Salary | Date of Joining |
  20. |-------------|-----|---------------|--------|-----------------|
  21. | Rohan Gupta | 28  | Data Analyst  | 55000  | 2021-04-10      |
  22. | Priya Sharma| 32  | HR Manager    | 65000  | 2020-11-05      |
  23. | Neha Verma  | 25  | Software Eng  | 72000  | 2022-01-15      |
  24. | Aarav Patel | 29  | Accountant    | 60000  | 2021-06-25      |
  25. | Kiran Rao   | 31  | Marketing Head| 85000  | 2019-09-12      |
  26.  
  27. ### 1. **Reading an Excel File**
  28.  
  29. ```python
  30. import openpyxl
  31.  
  32. # Load an existing Excel workbook
  33. wb = openpyxl.load_workbook('employees.xlsx')
  34.  
  35. # Select the active worksheet
  36. ws = wb.active
  37.  
  38. # Read all rows
  39. for row in ws.iter_rows(values_only=True):
  40.     print(row)
  41. ```
  42.  
  43. ### 2. **Accessing a Specific Sheet**
  44.  
  45. ```python
  46. # Access a specific sheet by name
  47. ws = wb['Sheet1']
  48.  
  49. # Print the sheet title
  50. print(ws.title)
  51. ```
  52.  
  53. ### 3. **Writing to a New Excel File**
  54.  
  55. ```python
  56. # Create a new Excel workbook
  57. new_wb = openpyxl.Workbook()
  58.  
  59. # Select the active worksheet
  60. ws = new_wb.active
  61.  
  62. # Add data to cells
  63. ws['A1'] = "Name"
  64. ws['B1'] = "Age"
  65. ws['A2'] = "Ravi Kumar"
  66. ws['B2'] = 28
  67.  
  68. # Save the workbook
  69. new_wb.save('new_employees.xlsx')
  70. ```
  71.  
  72. ### 4. **Modifying Cell Values**
  73.  
  74. ```python
  75. # Modify a specific cell
  76. ws['B2'] = 30
  77.  
  78. # Save the updated file
  79. wb.save('employees.xlsx')
  80. ```
  81.  
  82. ### 5. **Appending Rows to the Excel File**
  83.  
  84. ```python
  85. # Append a new row of data
  86. new_employee = ['Rahul Singh', 27, 'Graphic Designer', 50000, '2022-09-15']
  87. ws.append(new_employee)
  88.  
  89. # Save the updated file
  90. wb.save('employees.xlsx')
  91. ```
  92.  
  93. ### 6. **Iterating Over Rows and Columns**
  94.  
  95. ```python
  96. # Iterate over rows and print all data
  97. for row in ws.iter_rows(min_row=2, max_row=6, min_col=1, max_col=5, values_only=True):
  98.     print(row)
  99. ```
  100.  
  101. ### 7. **Inserting Columns and Rows**
  102.  
  103. ```python
  104. # Insert a new column before the first column
  105. ws.insert_cols(1)
  106.  
  107. # Insert a new row at position 3
  108. ws.insert_rows(3)
  109.  
  110. # Save changes
  111. wb.save('employees.xlsx')
  112. ```
  113.  
  114. ### 8. **Deleting Columns and Rows**
  115.  
  116. ```python
  117. # Delete the second column (B)
  118. ws.delete_cols(2)
  119.  
  120. # Delete the fourth row
  121. ws.delete_rows(4)
  122.  
  123. # Save changes
  124. wb.save('employees.xlsx')
  125. ```
  126.  
  127. ### 9. **Merging Cells**
  128.  
  129. ```python
  130. # Merge cells from A1 to C1
  131. ws.merge_cells('A1:C1')
  132.  
  133. # Save changes
  134. wb.save('employees.xlsx')
  135. ```
  136.  
  137. ### 10. **Unmerging Cells**
  138.  
  139. ```python
  140. # Unmerge the previously merged cells
  141. ws.unmerge_cells('A1:C1')
  142.  
  143. # Save changes
  144. wb.save('employees.xlsx')
  145. ```
  146.  
  147. ### 11. **Formatting Cells**
  148.  
  149. ```python
  150. from openpyxl.styles import Font, Alignment
  151.  
  152. # Apply bold font to a cell
  153. ws['A1'].font = Font(bold=True)
  154.  
  155. # Apply center alignment to a cell
  156. ws['B1'].alignment = Alignment(horizontal='center')
  157.  
  158. # Save changes
  159. wb.save('employees.xlsx')
  160. ```
  161.  
  162. ### 12. **Setting Column Widths**
  163.  
  164. ```python
  165. # Set the width of column A
  166. ws.column_dimensions['A'].width = 20
  167.  
  168. # Save changes
  169. wb.save('employees.xlsx')
  170. ```
  171.  
  172. ### 13. **Setting Row Heights**
  173.  
  174. ```python
  175. # Set the height of the first row
  176. ws.row_dimensions[1].height = 25
  177.  
  178. # Save changes
  179. wb.save('employees.xlsx')
  180. ```
  181.  
  182. ### 14. **Freezing Panes**
  183.  
  184. ```python
  185. # Freeze the first row
  186. ws.freeze_panes = 'A2'
  187.  
  188. # Save changes
  189. wb.save('employees.xlsx')
  190. ```
  191.  
  192. ### 15. **Applying Conditional Formatting**
  193.  
  194. ```python
  195. from openpyxl.formatting.rule import ColorScaleRule
  196.  
  197. # Apply color scale based on Salary
  198. rule = ColorScaleRule(start_type="percentile", start_value=10, start_color="FF0000",
  199.                       mid_type="percentile", mid_value=50, mid_color="FFFF00",
  200.                       end_type="percentile", end_value=90, end_color="00FF00")
  201. ws.conditional_formatting.add('D2:D6', rule)
  202.  
  203. # Save changes
  204. wb.save('employees.xlsx')
  205. ```
  206.  
  207. ### 16. **Adding Formulas**
  208.  
  209. ```python
  210. # Add a formula to calculate the average salary
  211. ws['F1'] = 'Average Salary'
  212. ws['F2'] = '=AVERAGE(D2:D6)'
  213.  
  214. # Save changes
  215. wb.save('employees.xlsx')
  216. ```
  217.  
  218. ### 17. **Protecting a Worksheet**
  219.  
  220. ```python
  221. # Protect the worksheet with a password
  222. ws.protection.sheet = True
  223. ws.protection.password = 'mypassword'
  224.  
  225. # Save changes
  226. wb.save('employees.xlsx')
  227. ```
  228.  
  229. ### 18. **Creating Charts (Bar Chart)**
  230.  
  231. ```python
  232. from openpyxl.chart import BarChart, Reference
  233.  
  234. # Create a bar chart for salary data
  235. chart = BarChart()
  236. data = Reference(ws, min_col=4, min_row=1, max_row=6)
  237. chart.add_data(data, titles_from_data=True)
  238. ws.add_chart(chart, 'H5')
  239.  
  240. # Save changes
  241. wb.save('employees.xlsx')
  242. ```
  243.  
  244. ### 19. **Adding Images to Excel**
  245.  
  246. ```python
  247. from openpyxl.drawing.image import Image
  248.  
  249. # Load and insert an image
  250. img = Image('logo.png')
  251. ws.add_image(img, 'G1')
  252.  
  253. # Save changes
  254. wb.save('employees.xlsx')
  255. ```
  256.  
  257. ### 20. **Saving a Workbook with Different Names**
  258.  
  259. ```python
  260. # Save the workbook under a new name
  261. wb.save('updated_employees.xlsx')
  262. ```
  263.  
  264. ### Real-Life Use Case: Employee Management System
  265.  
  266. Consider that your small company in India is managing employees’ salary and performance data using Excel. The `openpyxl` library allows automating the management and updating of employee records. From modifying records, generating performance reports, adding charts, to applying conditional formatting, `openpyxl` is a powerful tool for such tasks.
  267.  
  268. In the above example, you manage a simple employee list, automate the addition of new employees, calculate average salary, and apply some formatting rules.
  269.  
  270. With `openpyxl`, these tasks can be extended to real-world scenarios such as:
  271. - Managing payroll data for employees like Rohan Gupta, Priya Sharma, and Neha Verma.
  272. - Automating exam score analysis for students in a class.
  273. - Generating sales reports for a small business.
  274.  
  275. This set of examples gives a foundation for handling Excel files in Python using `openpyxl`, opening up possibilities for business automation, reporting, and data analysis.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement