use data from excel file in another python script - python

I want to write a python script that takes data from one excel file and uses this data and inputs it in another excel file to get the output. For eg, if i have input.csv, it takes the data from there, and replaces certain cells of output.csv and gets the value based of the calculation
import pandas as pd
import numpy as np
data=pd.read_excel("Data.xlsx")
Depth=data["Depth (D):"];
ID=data["Tubing inner diameter (dti):"];
API=data["Oil gravity (API):"];
oilvisc=data["Oil viscosity (cp):"];
this is the script i have currently, these are the inputs.
import xlwt
import xlrd
from xlutils.copy import copy
rb=xlrd.open_workbook("hagedornbrowncorrelation.xls")
wb=copy(rb)
w_sheet=wb.get_sheet(0)
w_sheet.write(4,2,700)
wb.save("hagedornbrowncorrelation.xls")
the workbook "hagedornbrowncorrelation.xls" is my calculator, i am replacing the C5 with 700, but when i save it, all the macros and formulas in the workbook just go away and it becomes a useless workbook with numbers

I have done a similar project with openpyxl module which can be found here
https://openpyxl.readthedocs.io/en/stable/
Because I build a UI with Tkinter, I did to open a file, you may not want to use a global variable like I did, this was a quick hack.
def getFilecurrent():
global path
# open dialog box to select file
path = filedialog.askopenfilename(initialdir="/", title="Select file")
Then you can store it using
ref_workbook = openpyxl.load_workbook("filevariable")
Then do your manipulation of the data by selecting the right cell using, also remember to select the right worksheet.
weeklyengagement = ws['B18'].value
Afterwards, you create a new template for the file pasted into like
template = openpyxl.load_workbook("Section12Grades.xlsx") #Add file name
temp_sheet = template.get_sheet_by_name("Sheet1") #Add Sheet name
Lastly, you copy the range and paste the range using loops. There are so many resources out there I'm not going to paste my code as it has some custom set up and it would only confuse you.
Edit: if you wish to save with Macro, you can do:
wb = load_workbook(filename='filename.xlsm', read_only=False, keep_vba=True)
Formulas are string and if you wish to save the formulas, you have to keep it in the string format and save.

Related

Read an excel file with Python and modify it without changing the style

I have a excel file which has specific format, font and color etc. I would like to read it using Python libraries like pandas (pd.read_excel) and just modify a few cells without affecting the style. Is it possible? Currently, when I read and write using pandas, the style changes and it seems difficult to make the complex style in Python again.
Is there a way to load and store the format/style of Excel file when we are reading it, to be applied when it is being saved? I just want to modify the value of few cells.
You can use the openpyxl library like this:
from openpyxl import Workbook, load_workbook
workbook = load_workbook("test.xlsx") # Your Excel file
worksheet = workbook.active # gets first sheet
for row in range(1, 10):
# Writes a new value PRESERVING cell styles.
worksheet.cell(row=row, column=1, value=f'NEW VALUE {row}')
workbook.save("test.xlsx")
You can use the set_properties() function.
Further use can be viewed at How to change the font-size of text in dataframe using pandas

Edit .xlsx with python

I Completely have no idea where to start.
I want to edit some think like:
To:
I want to save the result in a .txt file.
Every thing i know is to open and read the file.
code:
import pandas as pd
file = "myfile.xlsx"
f = pd.read_excel(file)
print(f)
I think the image colors speak for themselves how the code have to run. If not, I'll answer any question.
My go-to for editing Excel spreadsheets is openpyxl
I don't believe it can turn .csv or .xlsx/xlsm into .txt files, but it can read .xlsx/xlsm and save them as a .csv, and pandas can read csv files, so you can probably go from there
Quick example:
from openpyxl import load_workbook
wb = load_workbook("foo.xlsx")
sheet = wb["baz"]
sheet["D5"] = "I'm cell D5"
Use openpyxl, and look at this below:
Get cell color from .xlsx
color_in_hex = sh['A2'].fill.start_color.index # this gives you Hexadecimal value of the color (in cell A2)
So you'd have to iterate across your columns/rows checking for a colour match, then if its a match, grab the value and apply it to your new sheet

Python: Modifying XLSM-sheet without loosing VBA scripts or shapes on other sheets

I want to modify an existing XLSM file, but:
I need to keep the VBA-Scripts
I need to keep the shapes (aka my "buttons" to trigger the scripts)
Further information:
So far I used openpyxl (but am open to other modules)
I want to modify sheet_1 only - it contains only data
sheet_2 contains shapes as buttons with VBA-macros behind them
sheet_3 to sheet_n contain more data without any shapes/linked scripts.
I managed to keep the vba scripts within the XLSM file by using the parameter "keep_vba", but it still deletes any shapes in the workbook. And since the shapes are used as buttons, I cannot delete them.
My code currently looks like follows.
from openpyxl import load_workbook
workbook = load_workbook(filename=insert_file, read_only=False, keep_vba=True)
import_sheet = workbook["sheet_1"]
row_array = ["1", "2", "3", "4"]
# get first empty row based on the key-column (=A)
empty_row_index = None
for i in range(1, 500, 1):
if import_sheet['A'+str(i)].value is None:
empty_row_index = i
break
for i, val in enumerate(row_array): # insert row
import_sheet.cell(row=empty_row_index, column=i+1).value = val
workbook.save(insert_file)
Any help / suggestions are welcome.
It turns out that xlwings can do what I want - it doesn't specify it in their documentation, but it works nevertheless. The following code snippet opens an xlsm file, inserts data into a cell and overwrites the workbook while keeping both macros and shapes/"buttons".
pip install xlwings
import xlwings as xw
wb = xw.Book(r'C:\path\to\file\file.xlsm')
sht = wb.sheets['sheet_1']
sht.range('A5').value = 'Foo 1'
wb.save(r'C:\path\to\file\file.xlsm') # full path overwrites file without prompt
# optional to close the appearing excel window
app = xw.apps.active
app.quit()

Append output to an existing Excel File using OpenPyXL

Step 1. Take input from an excel file.
Step 2. Ceate WB object.
Step 3. Take data from that WB object and create a list of dictionaries.
Step 4. Manipulate, Format, style the data.
Step 5. Now need to output the data and APPEND it into an Existing Excel Workbook.
Meaning the Existing Excel Workbook will be appended with the new data from
Step 1 constantly.
I have Steps 1 - 4 down and can output my data to a NEW workbook. I am coming up
empty on Step 5.
Any guidance or direction would be appreciated.
### Python 3.X ###
import sys
import time
import openpyxl
from openpyxl.styles import Alignment, Font, Style
from openpyxl.cell import get_column_letter
from pathlib import Path
###Double Click a Batch file, CMD opens, Client drags and drops excel File
###to be Formatted
try:
path = sys.argv[1]
except IndexError:
path = Path(input('Input file to read: ').strip("'").strip('"'))
output_string = str(Path(path.parent, path.stem + '.NewFormatted.xlsx'))
wb = openpyxl.load_workbook(str(path))
sheet1 = wb.worksheet[0]
###Do the Python Dance on Data###
###Style, Font, Alignment applied to sheets###
###Currently Saves output as a NEW excel File in the Directory the original
###file was drag and dropped from
wb.save(output_string)
###Need the output to be appended to an existing Excel file already present
###in a target directory
###Note Formatted Workbook (output) has multiple sheets###
openpyxl will let you edit existing workbooks including appending data to them. But the methods provided are limited to individual items of data such as the cells. There are no aggregate functions for copying things like worksheets from one workbook to another.

How to preserve macro buttons in Excel when adding data with Python

For a process I am maintaining, I have a script that creates a csv file and then I copy the csv file into an Excel workbook with buttons that activate macros. This process works just fine.
I am trying to improve that process by writing a script that builds the workbook directly, thus eliminating a step. I thought the best way to do that was to create a template workbook where the first worksheet has the macro button. Then I would simply copy the template workbook, add in my data and save the new workbook under a new custom name. My test code is below:
import csv, os, sys, xlrd, xlwt, xlutils, shutil
from copy import deepcopy
from xlutils import save
from xlutils.copy import copy
templatefile = 'N:\Tools\Scripts-DEV\Testing_Template.xls'
Destfile = 'N:\Tools\Scripts-DEV\Testing_Dest.xls'
shutil.copy(templatefile,Destfile)
# Works fine up to here.
# If you look at the new file, it has the button that is in the template file.
rb = xlrd.open_workbook(Destfile)
rs = rb.sheet_by_index(0)
wb = copy(rb)
wb.get_sheet(0).write(3, 0, 'Due Date')
wb.get_sheet(0).write(3, 1, 'Name')
wb.get_sheet(0).write(3, 3, 'Category')
wb.get_sheet(0).write(3, 4, 'Number')
wb.save(Destfile)
Here is where the problem shows up. After you save, the macro button disappears. I've been looking for a couple days but I haven't (yet) found a way to save the updated Excel file without losing the macro button.
I've been looking at Preserving styles using python's xlrd,xlwt, and xlutils.copy but that doesn't quite meet my needs as I'm trying to preserve a button, not a style.
Does anyone know a way to do this?
I'm about to start looking at alternatives to xlutils, xlrd and xlwt as well, but I thought I'd ask here first.
From you comment part C:\Python27\ I deduce that you are on Windows. In that case you are probably better off with using pywin32 and a template .xls or .xlsm file.
Open the file using os.startfile(filename) then connect using workbook = win32com.client.GetObject(filename). The resulting workbook can be filled with the data and written to a new file with `workbook.SaveAs(newfilename).
Anything you do not touch explicitly is preserved. Excel is, of course, somewhat better at that than xlrd, xlwt and xlutils.

Categories

Resources