Openpyxl creates a function that refers to a cell on another sheet

I just started working with openpyxl a couple of days ago and its excellent library. However, the documentation is apparently rare for advanced features. I have a couple of questions.

  • openpyxl seems to change the formula that I paste in lowercase, which results in an unknown link from excel.
  • Also, I changed the name of the sheet to accommodate lower case and still found #NAME? error in the cell where the link was.

Can someone please show me how and where can I find out how to reference a cell from another sheet in openpyxl

import openpyxl.Workbook wb = Workbook() ws = wb.get_active_sheet() #shows up lowercase with name error in excel ws.cell('A1).value = "$'Sheet'.E7 + 123" #still shows a name error in excel ws.cell('A2').value = "$'sheet'.E7 + 123" 
+7
source share
2 answers

Try the following:

 from openpyxl import Workbook wb = Workbook() ws = wb.create_sheet() ws.title ='NewSheet' ws.cell('E7').value = 7 ws = wb.create_sheet() ws.cell('A1').value = "=NewSheet!E7 + 123" wb.save( filename = 'temp2.xlsx' ) 
+4
source
 from openpyxl import Workbook, utils wb = Workbook() ws = wb.create_sheet() ws.title ='NewSheet' ws.cell('E7').value = 7 ws = wb.create_sheet() ws.cell('A1').value = f"={utils.quote_sheetname(ws.title)}!E7 + 123" wb.save( filename = 'temp2.xlsx' ) 

The problem with the previous answer is that it depends on the title of the NewSheet. Using quote_sheetname() controls this.

0
source

Source: https://habr.com/ru/post/951749/


All Articles