info:cursos:python:openpyxl

Diferències

Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.

Enllaç a la visualització de la comparació

Ambdós costats versió prèvia Revisió prèvia
Següent revisió
Revisió prèvia
info:cursos:python:openpyxl [08/10/2021 00:46] – [Curso python + openpyxl (excel)] mateinfo:cursos:python:openpyxl [10/06/2022 02:28] (actual) – [Curso python + openpyxl (excel)] mate
Línia 1: Línia 1:
 = Curso python + openpyxl (excel) = Curso python + openpyxl (excel)
 +{{tag>python}}
   * udemy   * udemy
-  * [[https://www.udemy.com/course/python-learn-how-to-read-write-and-manipulate-excel-files/learn/lecture/8080204#overview]]+  * [[https://www.udemy.com/course/python-learn-how-to-read-write-and-manipulate-excel-files/learn/lecture/8080232#overview]]
  
 == 2.Python - Working with Sheet == 2.Python - Working with Sheet
Línia 22: Línia 23:
 sheet.max_column sheet.max_column
 sheet.title='nou nom' sheet.title='nou nom'
-sheet.save('file.xlsx')+wb.save('file.xlsx')
 </code> </code>
 +
 +<code python>
 +wb.create_sheet(index=<posición>,title='titol')
 +
 +std=wd.get_sheet_by_name('titol')
 +wb.remove_sheet(std)
 +
 +wb.save('file.xlsx')
 +</code>
 +
 +<code python>
 +sheet=wb.get_sheet_by_name('pestanya')
 +sheet['B2'].value
 +sheet['B2']='nou valor'
 +
 +e=sheet['B2']
 +e.value
 +e.row
 +e.column
 +e.value='nou valor'
 +
 +e=sheet.cell(row=2,column=2)
 +
 +wb.save('file.xlsx')
 +</code>
 +
 +== 3.Reading and Writing Rows in Excel
 <code python> <code python>
 for i in range(1,sheet.max_column+1): for i in range(1,sheet.max_column+1):
Línia 33: Línia 61:
   e=sheet.cell(row=5,column=i)   e=sheet.cell(row=5,column=i)
   lista.append(e.value)   lista.append(e.value)
 +</code>
 +<code python>
 +for i in range(0,len(lista)):
 +  e=sheet.cell(row=10,column=7+i)
 +  e.value=list[i]
 +  
 +wb.save('file.xlsx')
 </code> </code>
  
 +== 4.Reading and Writing Column and later Grid in a Sheet
 +<code python>
 +for i in range(1,sheet.max_row+1):
 +  e=sheet.cell(row=j,column=2)
 +  print(e.value)
 +  
 +lista = []
 +for i in range(1,5):
 +  e=sheet.cell(row=i,column=2)
 +  lista.append(e.value)
 +  
 +wb2=openpyxl.load_workbook('file2.xlsx')
 +sheet2=wb2.get_sheet_by_name('Sheet3')
 +for i in range(0,len(lista)):
 +  e=sheet2.cell(row=10+i,column=2)
 +  e.value=listb[i]
 +  
 +wb2.save('file2.xlsx')
 </code> </code>
 +<code python>
 +### define 2D list
 +rows=6
 +columns=4
 +lista=[]
 +for i in range(1,rows+1):
 +  lista.append([]) # add list into a list
  
-== 3.Reading and Writing Rows in Excel+# read data 
 +for r in range(1,rows+1): 
 +  for c in range(1,columns+1): 
 +    e=sheet.cell(row=r,column=c) 
 +    lista[r-1].append(e.value) 
 + 
 +# write data 
 +for r in range(1,rows+1): 
 +  for c in range(1,columns+1): 
 +    j=sheet2.cell(row=r,column=c) 
 +    j.value=lista[r-1][c-1] 
 +     
 +# save 
 +</code>
  • info/cursos/python/openpyxl.1633679184.txt.gz
  • Darrera modificació: 08/10/2021 00:46
  • per mate