Opening specific Excel sheet with Python when opening application - python

Is there a way to specify which sheet to open within an excel workbook when using a python command to open the application? (ex: using win32 Dispatch or os.system)?

I think the best way would be to activate the focus on the sheet first, then open the workbook.
from openpyxl import load_workbook
wb = load_workbook('my_workbook.xlsx')
sheet_to_focus = 'my_sheet'
for s in range(len(wb.sheetnames)):
if wb.sheetnames[s] == sheet_to_focus:
break
wb.active = s
wb.save('my_workbook.xlsx')
Then you could probably open it (untested code):
import os
os.chdir('C:\\my_folder\\subfolder')
os.system('start excel.exe my_workbook.xlsx')

I find the easiest way to be with pandas:
import pandas as pd
df = pd.read_excel('path/to/sheet.xlsx', 'sheet_name')
You can read the documentation here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

Related

Load sheet in an excel file and save it to another different excel file

Using python 3, I'm trying to append a sheet from an existing excel file to another excel file.
I have conditional formats in this excel file so I can't just use pandas.
from openpyxl import load_workbook
final_wb = load_workbook("my_final_workbook_with_lots_of_sheets.xlsx")
new_wb = load_workbook("workbook_with_one_sheet.xlsx")
worksheet_new = new_wb['Sheet1']
final_wb.create_sheet(worksheet_new)
final_wb.save("my_final_workbook_with_lots_of_sheets.xlsx")
This code doesn't work because the .create_sheet method only makes a new blank sheet, I want to insert my worksheet_new that I loaded from the other file into the final_wb
See this issue on openpyxl discussing this same feature: https://bitbucket.org/openpyxl/openpyxl/issues/171/
It is not currently a builtin function of openpyxl, but that thread has some workarounds available that may suit your needs.
EDIT: Also found a likely duplicate question here: Copy worksheet from one workbook to another one using Openpyxl
I found a solution using pandas.ExcelWriter, which also uses xlrd and openpyxl in the background:
I created two sample excel-files:
test1.xlsx
test2.xlsx
and append the first sheet of test1.xlsx to test2.xlsx:
In [1]: import pandas as pd
In [2]: from pandas import ExcelWriter
In [3]: with ExcelWriter("test2.xlsx", mode="a") as writer:
...: df1 = pd.read_excel("test1.xlsx", sheet_name=0)
...: df1.to_excel(writer, sheet_name="New sheet name")
Important is the mode="a", which toggles the append-mode.

Python: Open Excel Workbook If it Exists or Create it

I am trying to get a good method to see if an Excel spreadsheet exists, if it does use that, if not create a new excel file. See code snippet below. The weird thing is every time I run it, it crashed on first attempt. If I run it again, it cruises through. Any ideas why? I am thinking it has something to do with xlrd vs. xlwt, but haven't found a solution yet. All modules are up-to-date.
import pandas as pd
import xlsxwriter
from xlrd import open_workbook
import xlwt
import os.path
fname=r'testmonthlyz.xlsm'
fname2=r'testmonthlyoutput2.xlsx'
#workbook = xlsxwriter.Workbook(fname2)
if os.path.isfile(fname2):
print('old file')
book=open_workbook(fname2)
else:
print('new file')
book=xlwt.Workbook(fname2)
ws = book.add_sheet('Tested')
sheet_names=book.sheet_names()
I believe that the reason that is crashed is since when you are in the else section, you have the line book=xlwt.Workbook(fname2) which means the book type is Workbook which has no attribute called sheet_names().
When you are using book = open_workbook(fname2) inside the if, book type is Book which does have sheet_names() attribute.
my solution to this, even though is's not the best way, but I think it will solve the issue you are dealing with..
change the following lines
import pandas as pd
import xlsxwriter
from xlrd import open_workbook
import xlwt
import os.path
fname=r'testmonthlyz.xlsm'
fname2=r'testmonthlyoutput2.xlsx'
workbook = xlsxwriter.Workbook(fname2)
if os.path.isfile(fname2):
print('old file')
book=open_workbook(fname2)
else:
print('new file')
workbook2=xlwt.Workbook(fname2)
ws = workbook2.add_sheet('Tested')
workbook2.save(fname2)
book = open_workbook(fname2)
sheet_names=book.sheet_names()

Python openpyxl data_only=True returning None

I have a simple excel file:
A1 = 200
A2 = 300
A3 = =SUM(A1:A2)
this file works in excel and shows proper value for SUM, but while using openpyxl module for python I cannot get value in data_only=True mode
Python code from shell:
wb = openpyxl.load_workbook('writeFormula.xlsx', data_only = True)
sheet = wb.active
sheet['A3']
<Cell Sheet.A3> # python response
print(sheet['A3'].value)
None # python response
while:
wb2 = openpyxl.load_workbook('writeFormula.xlsx')
sheet2 = wb2.active
sheet2['A3'].value
'=SUM(A1:A2)' # python response
Any suggestions what am I doing wrong?
It depends upon the provenance of the file. data_only=True depends upon the value of the formula being cached by an application like Excel. If, however, the file was created by openpyxl or a similar library, then it's probable that the formula was never evaluated and, thus, no cached value is available and openpyxl will report None as the value.
I have replicated the issue with Openpyxl and Python.
I am currently using openpyxl version 2.6.3 and Python 3.7.4. Also I am assuming that you are trying to complete an exercise from ATBSWP by Al Sweigart.
I tried and tested Charlie Clark's answer, considering that Excel may indeed cache values. I opened the spreadsheet in Excel, copied and pasted the formula into the same exact cell, and finally saved the workbook. Upon reopening the workbook in Python with Openpyxl with the data_only=True option, and reading the value of this cell, I saw the proper value, 500, instead of the wrong value, the None type.
I hope this helps.
I had the same issue. This may not be the most elegant solution, but this is what worked for me:
import xlwings
from openpyxl import load_workbook
excel_app = xlwings.App(visible=False)
excel_book = excel_app.books.open('writeFormula.xlsx')
excel_book.save()
excel_book.close()
excel_app.quit()
workbook = load_workbook(filename='writeFormula.xlsx', data_only=True)
I have suggestion to this problem. Convert xlsx file to csv :).
You will still have the original xlsx file. The conversion is done by libreoffice (it is that subprocess.call() line).You can use also Pandas for this as a more pythonic way.
from subprocess import call
from openpyxl import load_workbook
from csv import reader
filename="test"
wb = load_workbook(filename+".xlsx")
spread_range = wb['Sheet1']
#what ever function there is in A1 cell to be evaluated
print(spread_range.cell(row=1,column=1).value)
wb.close()
#this line can be done with subprocess or os.system()
#libreoffice --headless --convert-to csv $filename --outdir $outdir
call("libreoffice --headless --convert-to csv "+filename+".xlsx", shell=True)
with open(filename+".csv", newline='') as f:
reader = reader(f)
data = list(reader)
print(data[0][0])
or
# importing pandas as pd
import pandas as pd
# read an excel file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_excel("Test.xlsx"))
# show the dataframe
df
I hope this helps somebody :-)
Yes, #Beno is right. If you want to edit the file without touching it, you can make a little "robot" that edits your excel file.
WARNING: This is a recursive way to edit the excel file. These libraries are depend on your machine, make sure you set time.sleep properly before continuing the rest of the code.
For instance, I use time.sleep, subprocess.Popen, and pywinauto.keyboard.send_keys, just add random character to any cell that you set, then save it. Then the data_only=True is working perfectly.
for more info about pywinauto.keyboard: pywinauto.keyboard
# import these stuff
import subprocess
from pywinauto.keyboard import send_keys
import time
import pygetwindow as gw
import pywinauto
excel_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
excel_file_path = r"D:\test.xlsx"
def focus_to_window(window_title=None): # function to focus to window. https://stackoverflow.com/a/65623513/8903813
window = gw.getWindowsWithTitle(window_title)[0]
if not window.isActive:
pywinauto.application.Application().connect(handle=window._hWnd).top_window().set_focus()
subprocess.Popen([excel_path, excel_file_path])
time.sleep(1.5) # wait excel to open. Depends on your machine, set it propoerly
focus_to_window("Excel") # focus to that opened file
send_keys('%{F3}') # excel's name box | ALT+F3
send_keys('AA1{ENTER}') # whatever cell do you want to insert somthing | Type 'AA1' then press Enter
send_keys('Stackoverflow.com') # put whatever you want | Type 'Stackoverflow.com'
send_keys('^s') # save | CTRL+S
send_keys('%{F4}') # exit | ALT+F4
print("Done")
Sorry for my bad english.
As others already mentioned, Openpyxl only reads cashed formula value in data_only mode. I have used PyWin32 to open and save each XLSX file before it's processed by Openpyxl to read the formulas result value. This works for me well, as I don't process large files. This solution will work only if you have MS Excel installed on your PC.
import os
import win32com.client
from openpyxl import load_workbook
# Opening and saving XLSX file, so results for each stored formula can be evaluated and cashed so OpenPyXL can read them.
excel_file = os.path.join(path, file)
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
excel.DisplayAlerts = False # disabling prompts to overwrite existing file
excel.Workbooks.Open(excel_file )
excel.ActiveWorkbook.SaveAs(excel_file, FileFormat=51, ConflictResolution=2)
excel.DisplayAlerts = True # enabling prompts
excel.ActiveWorkbook.Close()
wb = load_workbook(excel_file)
# read your formula values with openpyxl and do other stuff here
I ran into the same issue. After reading through this thread I managed to fix it by simply opening the excel file, making a change then saving the file again. What a weird issue.

Overwriting existing cells in an XLSX file using Python

I am trying to find a library that overwrites an existing cell to change its contents using Python.
what I want to do:
read from .xlsx file
compare cell data determine if change is needed.
change data in cell Eg. overwrite date in cell 'O2'
save file.
I have tried the following libraries:
xlsxwriter
combination of:
xlrd
xlwt
xlutils
openpyxl
xlsxwriter only writes to a new excel sheet and file.
combination: works to read from .xlsx but only writes to .xls
openpyxl: reads from existing file but doesn't write to existing cells can only create new rows and cells, or can create entire new workbook
Any suggestions would greatly be appreciated. Other libraries? how to manipulate the libraries above to overwrite data in an existing file?
from win32com.client import Dispatch
import os
xl = Dispatch("Excel.Application")
xl.Visible = True # otherwise excel is hidden
# newest excel does not accept forward slash in path
wbs_path = r'C:\path\to\a\bunch\of\workbooks'
for wbname in os.listdir(wbs_path):
if not wbname.endswith(".xlsx"):
continue
wb = xl.Workbooks.Open(wbs_path + '\\' + wbname)
sh = wb.Worksheets("name of sheet")
sh.Range("A1").Value = "some new value"
wb.Save()
wb.Close()
xl.Quit()
Alternatively you can use xlwing, which (if I had to guess) seems to be using this approach under the hood.
>>> import xlwings as xw
>>> wb = xw.Book() # this will create a new workbook
>>> wb = xw.Book('FileName.xlsx') # connect to an existing file in the current working directory
>>> wb = xw.Book(r'C:\path\to\file.xlsx') # on Windows: use raw strings to escape backslashes

How to append to an existing excel sheet with XLWT in Python

I have created an excel sheet using XLWT plugin using Python. Now, I need to re-open the excel sheet and append new sheets / columns to the existing excel sheet. Is it possible by Python to do this?
After investigation today, (2014-2-18) I cannot see a way to read in a XLS file using xlwt. You can only write from fresh. I think it is better to use openpyxl. Here is a simple example:
from openpyxl import Workbook, load_workbook
wb = Workbook()
ws = wb.create_sheet()
ws.title = 'Pi'
ws.cell('F5').value = 3.14156265
wb.save(filename=r'C:\book2.xls')
# Re-opening the file:
wb_re_read = load_workbook(filename=r'C:\book2.xls')
sheet = wb_re_read.get_sheet_by_name('Pi')
print sheet.cell('F5').value
See other examples here: http://pythonhosted.org/openpyxl/usage.html (where this modified example is taken from)
You read in the file using xlrd, and then 'copy' it to an xlwt Workbook using xlutils.copy.copy().
Note that you'll need to install both xlrd and xlutils libraries.
Note also that not everything gets copied over. Things like images and print settings are not copied, for example, and have to be reset.

Categories

Resources