Mostra la pàginaRevisions anteriorsQuè hi enllaçaExporta a PDFTorna dalt Aquesta pàgina és només de lectura. Podeu veure'n el codi font, però no podeu canviar-la. Consulteu el vostre administrador si penseu que això és degut a algun error. = Curso python + openpyxl (excel) {{tag>python}} * udemy * [[https://www.udemy.com/course/python-learn-how-to-read-write-and-manipulate-excel-files/learn/lecture/8080232#overview]] == 2.Python - Working with Sheet <code python> import openpyxl import os os.getcwd() os.chdir(path) wb=openpyxl.load_workbook('...xlsx') type(wb) wb.get_sheet_names() </code> <code python> sheet=wb.get_sheet_by_name('pestanya') type(sheet) sheet.title sheet.max_row sheet.max_column sheet.title='nou nom' wb.save('file.xlsx') </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> for i in range(1,sheet.max_column+1): e=sheet.cell(row=1,column=i) print(e.value,end=' ') lista = [] for i in range(1,5): e=sheet.cell(row=5,column=i) 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> == 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 python> ### define 2D list rows=6 columns=4 lista=[] for i in range(1,rows+1): lista.append([]) # add list into a list # 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.txt Darrera modificació: 10/06/2022 02:28per mate