Can't create a new sheet in GoogleColab and make calculcation - python

I have an xlsx file on my computer with initial data (value) - two sheets were created. I decide to calculate some value in sheet1 and in sheet2. After this I try to save the result, but when I download a file from GoogleColab and get my start file with initial values.
And I don't know how to creat a new Sheet3 and make this calcuation in it: copy one column from Sheet1 and another column Sheet2 and make P_roz = P_nav + P-osvit the result in a new column in Sheet3.
How to download the xlsx file with the result in picture 2 and picture 4?
from google.colab import drive
drive.mount('/content/gdrive')
!pip install -q xlrd
import pandas as pd
import numpy as np
# Works with Sheet1
df_1 = pd.read_excel('my_path', name_sheet = 'Sheet1')
df_1['T_potyz'] = round((((1 / df_1['K_potyz']**2) - 1))**(1/2.0),3)
df_1['P_nav, кВт'] = df_1['P_ust, кВт']\
* df_1['K_poputy1']
df_1['Q_nav, кВАр'] = round(df_1['P_ust, кВт']\
* df_1['T_potyz'],3)
df_1['S_nav, кВА'] = round((df_1['P_nav, кВт']**2\
+ df_1['Q_nav, кВАр']**2)\
**(1/2.0),3)
df_1['P_nav, кВт'].sum(), df_1['Q_nav, кВАр'].sum(), df_1['S_nav, кВА'].sum()
sum_row1 = df_1[['P_nav, кВт', 'Q_nav, кВАр', 'S_nav, кВА']].sum()
sum_row1
# transpose the data and convert the Series to a DataFrame so that it is easier to concat onto our existing data.
#The T function allows us to switch the data from being row-based to column-based.
df_sum1 = pd.DataFrame(data = sum_row1).T
df_sum1
# to add the missing columns.
df_sum1 = df_sum1.reindex(columns = df_1.columns)
df_sum1
# add it to our existing one using append.
df_final_1 = df_1.append(df_sum1, ignore_index = True)
df_final_1.tail()
# Works with Sheet2
df_2 = pd.read_excel('my_path', sheet_name='Sheet2')
K_po = float(input("Коефіцієнт попиту загально освітлення: "))
df_2['P_ust'] = (round(df_2['k'] * df_2['P_put'] * 10**-3 *
df_2['A, м'] * df_2['B, м'],3))
df_2['P_osvit'] = round(K_po * df_2['P_ust'],3)
df_2['T_potyz2'] = round((((1 / df_2['K_poputy2']**2) - 1))**(1/2.0),3)
df_2['Q_osvit'] = round(df_2['P_osvit'] * df_2['T_potyz2'],3)
df_2['S_osvit'] = round((df_2['P_osvit']**2\
+ df_2['Q_osvit']**2)\
**(1/2.0),3)
df_2['P_osvit'].sum(), df_2['Q_osvit'].sum(), df_2['S_osvit'].sum()
sum_row2 = df_2[['P_osvit', 'Q_osvit', 'S_osvit']].sum()
sum_row2
df_sum2 = pd.DataFrame(data = sum_row2).T
df_sum2
df_sum2 = df_sum2.reindex(columns = df_2.columns)
df_sum2
df_final2 = df_2.append(df_sum2, ignore_index = True)
df_final2.tail()
# Here I want create a new Sheet3 and make some calculation
P_roz = df_1['P_nav, кВт'] + df_2['P_osvit']
Q_roz = df_1['Q_nav, кВАр'] + df_2['Q_osvit']
S_roz = (P_roz**2 + Q_roz**2)**(1 / 2.0)
frame_data3 = {'P_roz' : [P_roz], 'Q_roz' : [Q_roz], 'S_roz' : [S_roz]}
df_4 = pd.DataFrame(frame_data3)
df_4
Sheet 1 Sheet 1 + sum Sheet 2 Sheet 2 + sum

Related

Why openpyxl is hidding some rows?

I'm having a little trouble with openpyxl. I'm updating an excel with historic data something like this:
The excel has 2 sheets with this data, both are almost the same. The thing is that after I add the new rows and aply the formats, in the second sheet for some reason a lot of row are hidden, like this:
This is my code:
from copy import copy
import pandas as pd
from openpyxl import load_workbook
datos = pd.Dataframe(my_data) # This is not right but I already have the data in a DF
today = datos[col1].loc[0].strftime("%Y-%m-%d")
tomorrow = datos[col1].loc[1].strftime("%Y-%m-%d")
historic = 'my_excel.xlsx'
logger.info('Reading Excel')
wb = load_workbook(historic)
sheets = wb.sheetnames
for sheet in sheets:
logger.warning(f'************* {sheet} *************')
ws = wb[sheet]
logger.info(f'active: {wb.active}')
logger.info(f"len D: {len(ws['D'])}")
logger.info(f"len E: {len(ws['E'])}")
max_row = len(ws['D'])
logger.info('Last D cell')
last_D_cell = ws.cell(row=max_row, column=4)
last_D_cell_value = last_D_cell.value.strftime("%d/%m/%Y")
logger.info('New Cell')
new_D_cell = ws.cell(row=max_row + 1, column=4)
new_D_cell.font = copy(last_D_cell.font)
new_D_cell.border = copy(last_D_cell.border)
new_D_cell.number_format = copy(last_D_cell.number_format)
new_D_cell.alignment = copy(last_D_cell.alignment)
new_D_cell.fill = copy(last_D_cell.fill)
new_D_cell.value = datos[col1].loc[1].strftime("%d/%m/%Y")
logger.info('Penultimate D Cell')
penultimate_D_cell = ws.cell(row=max_row - 1, column=4)
last_D_cell.font = copy(penultimate_D_cell.font)
last_D_cell.border = copy(penultimate_D_cell.border)
last_D_cell.number_format = copy(penultimate_D_cell.number_format)
last_D_cell.alignment = copy(penultimate_D_cell.alignment)
last_D_cell.fill = copy(penultimate_D_cell.fill)
logger.info('Last E Cell')
last_E_cell = ws[f'E{max_row}']
new_E_cell = ws.cell(row=max_row + 1, column=5)
new_E_cell.font = copy(last_E_cell.font)
new_E_cell.border = copy(last_E_cell.border)
new_E_cell.number_format = copy(last_E_cell.number_format)
new_E_cell.alignment = copy(last_E_cell.alignment)
new_E_cell.fill = copy(last_E_cell.fill)
new_E_cell.value = tomorrow_value
logger.info('Penultimate E')
penultimate_E_cell = ws[f'E{max_row - 1}']
last_E_cell.font = copy(penultimate_E_cell.font)
last_E_cell.border = copy(penultimate_E_cell.border)
last_E_cell.number_format = copy(penultimate_E_cell.number_format)
last_E_cell.alignment = copy(penultimate_E_cell.alignment)
last_E_cell.fill = copy(penultimate_E_cell.fill)
logger.info('SAving Excel')
wb.save(historic)
With this code, the last sheet it works will have the hidden rows, and I don't know why is this happening.
Hope someone can help me thanks
EDIT: I'm on Ubuntu 20.4 LTS, and the resulting files has been opened in both Ubuntu and Windows 10 and the same situation appears.

How can I collapse or expand a pivot table in python using win32com

I'm creating a pivot table using the win32com.client module.
I'm managing to create it exactly as I want,
but the finishing touches that I need is to collapse the entire pivot table.
any suggestions?
This is my Code:
**
def insert_pt_field_set(pt):
field_rows = {}
field_rows['Period'] = pt.PivotFields('Period')
field_rows['Name'] = pt.PivotFields('Name')
field_values = {}
field_values['Name'] = pt.PivotFields("Name")
field_rows['Period'].Orientation = 1
field_rows['Period'].Position = 1
field_rows['Name'].Orientation = 1
field_rows['Name'].Position = 2
field_values['Name'].Orientation = 4
field_values['Name'].Function = -4112
xlApp = win32.Dispatch('Excel.Application')
xlApp.Visible = True
wb = xlApp.Workbooks.Open('output.xlsx')
ws = wb.Worksheets('Sheet1')
ws_pivot = wb.Worksheets('Pivot_Sheet1')
pt_cache = wb.PivotCaches().Create(1, ws.Range("A1").CurrentRegion)
pt = pt_cache.CreatePivotTable(ws_pivot.Range("B3"), "PivotName")
pt.ColumnGrand = True
pt.RowGrand = False
pt.SubtotalLocation(2)
pt.RowAxisLayout(2)
pt.TableStyle2 = "PivotStyleMedium2"
# Entering the function that arrange the fields in the pivot table
insert_pt_field_set(pt)
wb.Close(True)
**
Working in Excel 365
If you need to roll over the field:
ws.PivotTables("PivotName").PivotFields("SomeFiled").DrillTo("SomeFiled")
Or
ws.PivotTables("PivotName").PivotFields("SomeFiled").ShowDetail = False
If you just want to hide the fields:
ws.Columns('A:B').OutlineLevel = 2
ws.Columns('A:B').EntireColumn.Hidden = True
Similar question
VBA Pivot Table Collapse all fields
It says it's better to use "DrillTo". maybe
More information here
https://learn.microsoft.com/en-us/office/vba/api/excel.pivotfield.drillto

Is there a way to store output dataframes and appending them to the last output in the same dataframe

I am trying to fetch data from API for 50 parcels. I want them to be in a single data frame. While running this loop the data frame is storing only the last parcel which is satisfying the loop condition. Is there any way to store all the previous outputs also in the same dataframe.
For e.g upon running this code it only returns the data frame for foreign id=50, I want the dataframe for all 1-50.
import requests
import pandas as pd
foreign=1
while (foreign <=50):
s1_time_series_url_p6 = 'https://demodev2.kappazeta.ee/ard_api_demo/v1/time_series/s1?limit_to_rasters=true&parcel_foreign_id=0&properties=parcel_foreign_id%2Cs1product_end_time%2Cs1product_ron%2Ccohvh_avg%2Ccohvv_avg%2Cvhvv_avg'
s2_time_series_url_p6 = 'https://demodev2.kappazeta.ee/ard_api_demo/v1/time_series/s2?limit_to_rasters=true&parcel_foreign_id=0&properties=parcel_foreign_id%2Cs2product_start_time%2Cs2product_ron%2Cndvi_avg'
position = 101
foreign_n=str(foreign)
s1_time_series_url_p6 = s1_time_series_url_p6[:position] + foreign_n + s1_time_series_url_p6[position+1:]
s2_time_series_url_p6 = s2_time_series_url_p6[:position] + foreign_n + s2_time_series_url_p6[position+1:]
r_s1_time_series_p6 = requests.get(s1_time_series_url_p6)
r_s2_time_series_p6 = requests.get(s2_time_series_url_p6)
json_s1_time_series_p6 = r_s1_time_series_p6.json()
json_s2_time_series_p6 = r_s2_time_series_p6.json()
df_s1_time_series_p6 = pd.DataFrame(json_s1_time_series_p6['s1_time_series'])
df_s2_time_series_p6 = pd.DataFrame(json_s2_time_series_p6['s2_time_series'])
df_s2_time_series_p6.s2product_start_time=df_s2_time_series_p6.s2product_start_time.str[0:11]
df_s1_time_series_p6.s1product_end_time=df_s1_time_series_p6.s1product_end_time.str[0:11]
dfinal_p6 = df_s1_time_series_p6.merge(df_s2_time_series_p6, how='inner', left_on='s1product_end_time', right_on='s2product_start_time')
cols_p6 = ['parcel_foreign_id_x', 's1product_ron','parcel_foreign_id_y','s2product_ron']
dfinal_p6[cols_p6] = dfinal_p6[cols_p6].apply(pd.to_numeric, errors='coerce', axis=1)
dfinal_p6
The issue is resolved by first creating an empty data frame and then appending the outputs in the dataframe within the loop.
The updated code is as follows:
column_names = ["parcel_foreign_id_x", "s1product_end_time", "s1product_ron","cohvh_avg", "cohvv_avg", "vhvv_avg","parcel_foreign_id_y", "s2product_start_time", "s2product_ron", "ndvi_avg" ]
df = pd.DataFrame(columns = column_names)
foreign=1
while (foreign <=50):
s1_time_series_url_p6 = 'https://demodev2.kappazeta.ee/ard_api_demo/v1/time_series/s1?limit_to_rasters=true&parcel_foreign_id=0&properties=parcel_foreign_id%2Cs1product_end_time%2Cs1product_ron%2Ccohvh_avg%2Ccohvv_avg%2Cvhvv_avg'
s2_time_series_url_p6 = 'https://demodev2.kappazeta.ee/ard_api_demo/v1/time_series/s2?limit_to_rasters=true&parcel_foreign_id=0&properties=parcel_foreign_id%2Cs2product_start_time%2Cs2product_ron%2Cndvi_avg'
position = 101
foreign_n=str(foreign)
s1_time_series_url_p6 = s1_time_series_url_p6[:position] + foreign_n + s1_time_series_url_p6[position+1:]
s2_time_series_url_p6 = s2_time_series_url_p6[:position] + foreign_n + s2_time_series_url_p6[position+1:]
r_s1_time_series_p6 = requests.get(s1_time_series_url_p6)
r_s2_time_series_p6 = requests.get(s2_time_series_url_p6)
json_s1_time_series_p6 = r_s1_time_series_p6.json()
json_s2_time_series_p6 = r_s2_time_series_p6.json()
df_s1_time_series_p6 = pd.DataFrame(json_s1_time_series_p6['s1_time_series'])
df_s2_time_series_p6 = pd.DataFrame(json_s2_time_series_p6['s2_time_series'])
df_s2_time_series_p6.s2product_start_time=df_s2_time_series_p6.s2product_start_time.str[0:11]
df_s1_time_series_p6.s1product_end_time=df_s1_time_series_p6.s1product_end_time.str[0:11]
dfinal_p6 = df_s1_time_series_p6.merge(df_s2_time_series_p6, how='inner', left_on='s1product_end_time', right_on='s2product_start_time')
cols_p6 = ['parcel_foreign_id_x', 's1product_ron','parcel_foreign_id_y','s2product_ron']
dfinal_p6[cols_p6] = dfinal_p6[cols_p6].apply(pd.to_numeric, errors='coerce', axis=1)
df = pd.concat([dfinal_p6,df],ignore_index = True)
foreign = foreign+1

How to insert a real pivot table in a excel sheet with python?

I want to create a "real" pivot table in excel sheet with python without using the function of pandas (pandas.pivot_table).
Is there a method?
I need to create the "true" pivot table in excel with a dropdown combo box. Unfortunately the pandas pivot is static (is a table) and not dynamic.
Can anyone help me?
Can you try it this way?
import win32com.client
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Add()
Sheet1 = wb.Worksheets("Sheet1")
TestData = [['Country','Name','Gender','Sign','Amount'],
['CH','Max' ,'M','Plus',123.4567],
['CH','Max' ,'M','Minus',-23.4567],
['CH','Max' ,'M','Plus',12.2314],
['CH','Max' ,'M','Minus',-2.2314],
['CH','Sam' ,'M','Plus',453.7685],
['CH','Sam' ,'M','Minus',-53.7685],
['CH','Sara','F','Plus',777.666],
['CH','Sara','F','Minus',-77.666],
['DE','Hans','M','Plus',345.088],
['DE','Hans','M','Minus',-45.088],
['DE','Paul','M','Plus',222.455],
['DE','Paul','M','Minus',-22.455]]
for i, TestDataRow in enumerate(TestData):
for j, TestDataItem in enumerate(TestDataRow):
Sheet1.Cells(i+2,j+4).Value = TestDataItem
cl1 = Sheet1.Cells(2,4)
cl2 = Sheet1.Cells(2+len(TestData)-1,4+len(TestData[0])-1)
PivotSourceRange = Sheet1.Range(cl1,cl2)
PivotSourceRange.Select()
Sheet2 = wb.Worksheets(2)
cl3=Sheet2.Cells(4,1)
PivotTargetRange= Sheet2.Range(cl3,cl3)
PivotTableName = 'ReportPivotTable'
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('Name').Orientation = win32c.xlRowField
PivotTable.PivotFields('Name').Position = 1
PivotTable.PivotFields('Gender').Orientation = win32c.xlPageField
PivotTable.PivotFields('Gender').Position = 1
PivotTable.PivotFields('Gender').CurrentPage = 'M'
PivotTable.PivotFields('Country').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Country').Position = 1
PivotTable.PivotFields('Country').Subtotals = [False, False, False, False, False, False, False, False, False, False, False, False]
PivotTable.PivotFields('Sign').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Sign').Position = 2
DataField = PivotTable.AddDataField(PivotTable.PivotFields('Amount'))
DataField.NumberFormat = '#\'##0.00'
Excel.Visible = 1
wb.SaveAs('ranges_and_offsets.xlsx')
Excel.Application.Quit()
https://www.yinglinglow.com/blog/2018/04/29/xlwings-pivot-table
Here is another way using xlwings.
import xlwings as xw
from xlwings import constants
# create instance of Excel app
app = xw.App(visible=True)
# create workbook object
wb = app.books.open('my_file') # where my_file is in .xlsx format
# create sheet object
sht = wb.sheets['my_sheet_name'] # where my_sheet_name is name of your Excel sheet name that contains your data
# determine last row and column of your data
num_col = sht.range('A1').end('right').column
num_row = sht.range('A1').end('down').row
# define your pivot table data range
PivotSourceRange = sht.range((1,1), (num_row, num_col))
# add a pivot sheet to your Excel file
xw.sheets.add(name='pivot', before='my_sheet_name')
# name your pivot table
PivotTableName = 'MyPivotTable'
pivot_tab_name = '' + 'pivot' + '!R1C1'
# create pivot table cache
PivotCache = wb.api.PivotCaches().Create(SourceType=constants.PivotTableSourceType.xlDatabase,
SourceData=PivotSourceRange.api,
Version=constants.PivotTableVersionList.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=pivot_tab_name,
TableName=PivotTableName,
DefaultVersion=constants.PivotTableVersionList.xlPivotTableVersion14)
# structure the pivot table
PivotTable.PivotFields('your_row_label').Orientation = constants.PivotFieldOrientation.xlRowField
PivotTable.PivotFields('your_page_label').Orientation = constants.PivotFieldOrientation.xlPageField
DataField = PivotTable.AddDataField(PivotTable.PivotFields('the_column_you_want_to_tabulate'))
DataField.Function = constants.TotalsCalculation.xlTotalsCalculationAverage
# other available calculation methods include:
# .xlTotalsCalculationCountNums
# .xlTotalsCalculationMax
# .xlTotalsCalculationMin
# .xlTotalsCalculationNone
# .xlTotalsCalculationCount
# .xlTotalsCalculationCustom
# .xlTotalsCalculationStdDev
# .xlTotalsCalculationSum
# .xlTotalsCalculationVar
# you specify the formatting of your numbers with this statement
DataField.NumberFormat = '0.0' # which uses standard Excel formats
# finish building the pivot table
PivotTable.PivotFields('your_page_label').EnableMultiplePageItems = True
PivotTable.PivotFields('your_page_label').PivotItems().Visible = True
End result is something like this:

Python / Excel Automation - Text from 2 cells into 1 cell

I'm using python to automate the process of creating multiple name tags using an excel sheet list.
My problem is that I need to take the 'name' column and 'enterprise' column values ​​and put them in a single cell of a new document.
Like this:
To this:
Right now I'm using openpyxl and although I manage to transfer one of the columns I can't do it with both.
The code below was one of the things that I've tried.
import openpyxl as xl
e = xl.load_workbook('etiquetas.xlsx')
eplan = e['Planilha1']
c = xl.load_workbook('Crachá Relação 15.10.19.xlsx')
cplan = c['Plan1']
maxlinhas = cplan.max_row
for i in range (2, maxlinhas+1):
nome = cplan.cell(row = i, column = 1).value
preenchernome = eplan.cell(row = i-1, column = 1)
empresa = cplan.cell(row=i, column=2).value
preencherempresa = eplan.cell(row=i - 1, column=1)
preenchernome.value = nome, empresa
e.save('teste.xlsx')
But this code returns the following error:
ValueError: Cannot convert ('Gladisson Garcia Westphal', 'Agro Divel') to Excel
As per the docs preenchernome.value can only have one value
try to use this
preenchernome.value = '{}\n{}'.format(nome, empresa)
The value, that is passed on the target cell should be a single string. Thus:
wksTarget.cell(row = i, column = 1).value = '{}\n{}'.format(name, family)
should be ok. This is the whole code, that worked for me:
import openpyxl as xl
import os
wbSource = xl.load_workbook(os.path.dirname(os.path.realpath(__file__)) + '\myExcel.xlsx')
wksSourse = wbSource['Sheet1']
wbTarget = xl.load_workbook(os.path.dirname(os.path.realpath(__file__)) + '\Target.xlsx')
wksTarget = wbTarget['Sheet1']
for i in range (1, wksSourse.max_row+1):
name = wksSourse.cell(row = i, column = 1).value
family = wksSourse.cell(row = i, column = 2).value
wksTarget.cell(row = i, column = 1).value = '{}\n{}'.format(name, family)
wbTarget.save(os.path.dirname(os.path.realpath(__file__)) + '\Target.xlsx')
wbTarget.close()

Categories

Resources