About using Python to import Excel data into PPT tables - python

I want to try to import the data in Excel into the PPT table
The following is my code, but there is an error
import pandas as pd
import pptx
from openpyxl import load_workbook
from pptx.util import Pt, Inches, Cm
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.dml.color import RGBColor
wb = load_workbook('111.xlsx')
ws = wb['sheet2']
df = pd.read_excel('XXX.xlsx', sheet_name=0, usecols='A:P')
df = df.iloc[1:15]
# print(df)
df['Unnamed: 3'] = round(df['Unnamed: 3'].astype('float'), 0)
df['Unnamed: 5'] = df['Unnamed: 5'].apply(lambda x: format(x, '.2%'))
ppt = pptx.Presentation('demo.pptx')
layout = ppt.slide_layouts[12]
slide = ppt.slides.add_slide(layout)
rows = 14
cols = 16
left = top = Cm(2)
width = Cm(6.0)
height = Cm(1.0)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
for row in range(rows):
for col in range(cols):
table.cell(row, col).text = str(df[row][col])
ppt.save("test2.ppt")
Error display:
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "……", line 44, in <module>
table.cell(row, col).text = str(df[row][col])
File "……", line 3505, in __getitem__
indexer = self.columns.get_loc(key)
File "……", line 3623, in get_loc
raise KeyError(key) from err
KeyError: 0
I'm a new Python student. If this is an obvious question, don't mind. I appreciate your help

Related

ValueError: cannot insert column, already exists

I have a column called Soru-TR. There are two Turkish data in the column. What I want to do is to translate the data in the Soru-TR column and then save it under the Soru-EN column.
The error I get in the output.
Traceback (most recent call last):
File "C:\Users\User1\Desktop\test.py", line 17, in <module>
df.insert(1, "Soru-EN", output2)
File "C:\Users\User1\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 4443, in insert
raise ValueError(f"cannot insert {column}, already exists")
ValueError: cannot insert Soru-EN, already exists
before running the code
after running the code
import os
from typing import List
from openpyxl import load_workbook
import pandas as pd
from google.cloud import translate_v2
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"C:\Users\User1\Desktop\translate-598740482087.json"
translate_client = translate_v2.Client()
target = "en"
df = pd.read_excel('file.xlsx')
for i in df.index:
x = df['Soru-TR'][i]
output = translate_client.translate(x, target_language=target)
output2 = output['translatedText']
df.insert(1, "Soru-EN", output2)
with pd.ExcelWriter('file.xlsx', mode='a', engine="openpyxl", if_sheet_exists='overlay') as writer:
df.to_excel(writer, sheet_name='Sayfa1', index=False)
with pd.ExcelWriter('file.xlsx', mode='a', engine="openpyxl", if_sheet_exists='overlay') as writer:
df.to_excel(writer, sheet_name='Sayfa1', index=False)
try this
df = pd.read_excel('file.xlsx')
df["Soru-EN"] = "Pending" # add the column "Soru-EN" with value "Pending" for all rows
for i in df.index:
x = df.loc[i,'Soru-TR'] # tip: using pandas.loc[] to access values is better.
output = translate_client.translate(x, target_language=target)
output2 = output['translatedText']
df.loc[i, "Soru-EN"] = output2 # assign the translated text to its equivalent cell.
Hope this helps.

Copy Complete Excel row with values, formatting, formulas etc

Well I've a row which i want to copy complete.
I dont know how i can do that correctly in python.
i try to copy the code from another stackoverflow page. But if i test the code i get errors
my own code is this.
# Copy input to output
output_table = input_table.copy()
# conda install -c anaconda openpyxl
import os
import openpyxl
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from copy import copy
from copy import deepcopy
from openpyxl.styles import Font, colors, Color, PatternFill, Border, Side, Alignment, Protection
def move_cell(source_cell, dest_row, dest_col, preserve_original=False):
#param source_cell: cell to be moved to new coordinates
#param dest_row: 1-indexed destination row
#param dest_col: 1-indexed destination column
#param preserve_original: if True, does a copy instead of a move
if preserve_original:
cell_to_move = copy.copy(source_cell)
else:
cell_to_move = source_cell
worksheet = cell_to_move.parent
source_address = (cell_to_move.row, cell_to_move.col_idx)
dest_address = (dest_row, dest_col)
cell_to_move.row = dest_row
cell_to_move.col_idx = dest_col
worksheet._cells[dest_address] = cell_to_move
if not preserve_original:
del worksheet._cells[source_address]
return cell_to_move
var_data_path = "C:/"
#var_path_excel_file = var_data_path + os.path.sep + "data.xlsx"
wb = load_workbook(var_path_excel_file)
ws = wb["test"]
# activate the ws 'data'
for s in range(len(wb.sheetnames)):
if wb.sheetnames[s] == ws:
break
wb.active = s
mr = ws.max_row
mc = ws.max_column
# copying the cell values from source
# excel file to destination excel file
for j in range (1, mc + 1):
c = ws.cell(row = 10, column = j)
#ws.cells is my idea. But it don't copy the style and dont manipulate the formulas. i.e. =D10 to =D12 . Dependent from the row. Move cell function is the function which i copied from another great user. But i get errors for that. i test this all with the row 10. I want to copy the row 10 to 15. My error is function' object has no attribute 'copy' Traceback (most recent call #last): File "<string>", line 85, in <module>
# File "<string>", line 19, in move_cell
#AttributeError: 'function' object has no attribute 'copy'
ws.cell(15, column = j).value = c.value
move_cell(c, 15, j, preserve_original=True)
#ws[15] = ws[10]
wb.save(var_path_excel_file)
In openpyxl, how to move or copy a cell range with formatting, merged cells, formulas and hyperlinks

AttributeError: type object 'DataFrame' has no attribute 'pd'

import pandas as pd
import os
import time
from datetime import datetime
path = "C:/Users/Aben/Downloads/intraQuarter"
def Key_Stats(gather="Total Debt/Equity (mrq)"):
statspath = path+'/_KeyStats'
stock_list = [x[0] for x in os.walk(statspath)]
df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
sp500_df = pd.DataFrame.pd.read_csv("YAHOO-INDEX_CSPC.csv")
for each_dir in stock_list[1:]:
each_file = os.listdir(each_dir)
ticker = each_dir.split("\\")[1]
if len(each_file) > 0:
for file in each_file:
date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')
unix_time = time.mktime(date_stamp.timetuple())
full_file_path = each_dir+'/'+file
source = open(full_file_path,'r').read()
try:
value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])
try:
sp500_date = datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d')
row = sp500_df[sp500_df["Date"] == sp500_date]
sp500_value = float(row["Adj Close"])
except:
sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d')
row = sp500_df[sp500_df["Date"] == sp500_date]
sp500_value = float(row["Adj Close"])
stock_price = float(source.split('</small><big><b>')[1].split('</b></big>')[0])
print("stock_price:",stock_price,"ticker:",ticker)
df = df.append({'Date':date_stamp,'Unix':unix_time,'Ticker':ticker,'DE Ratio':value,}, ignore_index = True)
except Exception as e:
pass
save = gather.replace(' ','').replace('(','').replace(')','').replace('/','')+('.csv')
print(save)
df.to_csv(save)
Key_Stats()
when running this code in python 3.8.3, I get this error message:
Traceback (most recent call last):
File "C:\Users\Aben\Downloads\pythonstuff\stockbot.py", line 49, in <module>
Key_Stats()
File "C:\Users\Aben\Downloads\pythonstuff\stockbot.py", line 14, in Key_Stats
sp500_df = DataFrame.pd.read_csv("YAHOO-INDEX_CSPC.csv")
AttributeError: type object 'DataFrame' has no attribute 'pd'
anyone know why? I replaced from_csv with pd.read_csv, but now I have another error. The file I'm using is actually a txt file, but I renamed it with the .csv extension at the end.
I'm using this tutorial btw
Replace this line:
sp500_df = pd.DataFrame.pd.read_csv("YAHOO-INDEX_CSPC.csv")
with
sp500_df = pd.read_csv("YAHOO-INDEX_CSPC.csv")

How to convert txt to excel file

I have a txt file looks like this:
Cellname;Ncellname;Technology
52822;13621;GSM;
52822;13622;GSM;
52822;13623;GSM;
52822;16322;UMTS;
52822;16323;UMTS;
52822;16324;UMTS;
52822;16361;UMTS;
52822;16362;UMTS;
52822;16363;UMTS;
I tried to convert it using the below code:
import pandas as pd
import os
excel = 'gsmrelation_mnm.txt'
df = pd.read_csv(os.path.join(os.path.dirname(__file__), excel))
df.to_excel('gsmrelation_mnm.xlsx', 'Sheet1')
but I found this error:
Traceback (most recent call last):
File "C:/Users/haroo501/PycharmProjects/MyLiveRobo/convert_txt_csv.py", line 8, in <module>
df.to_excel('gsmrelation_mnm.xlsx', 'Sheet1')
File "C:\Users\haroo501\PycharmProjects\MyLiveRobo\venv\lib\site-packages\pandas\core\generic.py", line 2250, in to_excel
formatter.write(
File "C:\Users\haroo501\PycharmProjects\MyLiveRobo\venv\lib\site-packages\pandas\io\formats\excel.py", line 730, in write
writer = ExcelWriter(_stringify_path(writer), engine=engine)
File "C:\Users\haroo501\PycharmProjects\MyLiveRobo\venv\lib\site-packages\pandas\io\excel\_openpyxl.py", line 19, in __init__
from openpyxl.workbook import Workbook
ModuleNotFoundError: No module named 'openpyxl'
enter image description here
How to solve this problem
Try this:
import pandas as pd
excel = 'test.txt'
df = pd.read_csv(excel,sep=';')
column_indexes = list(df.columns)
df.reset_index(inplace=True)
df.drop(columns=df.columns[-1], inplace=True)
column_indexes = dict(zip(list(df.columns),column_indexes))
df.rename(columns=column_indexes, inplace=True)
df
and then
df.to_excel('output.xlsx', 'Sheet1')
and in case you don't want the indexes in the output sheet, use this
df.to_excel('output.xlsx', 'Sheet1', index=False)
import pandas as pd
file = pd.read_csv('input.txt', sep=';', index=False)
file.to_excel('output.xlsx', 'Sheet1')
I tried following,
finally what you expected
import pandas as pd
df = pd.read_csv('gsmrelation_mnm.txt', sep = ';', header=None, names=['Cellname', 'Ncellname', 'Technology'])
df.to_excel('gsmrelation_mnm.xlsx', 'Sheet1', index=False, header=None)

How can I use preset colors in openpyxl to do a PatternFill with a solid color

I'd like to avoid using hex codes for colors and take advantage of the preset color names in openpyxl to fill a cell. But I'm getting the following error with this code:
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, NamedStyle
from openpyxl.styles.colors import Color
from openpyxl.drawing.fill import ColorChoice
wb= Workbook()
ws = wb.active
sashighlight = NamedStyle(name="sashighlight")
bd = Side(style='thick', color="000000")
sashighlight.border = Border(left=bd, top=bd, right=bd, bottom=bd)
sashighlight.alignment = Alignment(horizontal='center', vertical='center')
sashighlight.fill = PatternFill(fill_type='solid', start_color=ColorChoice(prstClr='yellow'))
Traceback (most recent call last):
File "test.py", line 13, in <module>
sashighlight.fill = PatternFill(fill_type='solid', start_color=ColorChoice(prstClr='yellow'))
File "/usr/lib/python2.7/site-packages/openpyxl/styles/fills.py", line 92, in __init__
self.fgColor = fgColor
File "/usr/lib/python2.7/site-packages/openpyxl/styles/colors.py", line 126, in __set__
super(ColorDescriptor, self).__set__(instance, value)
File "/usr/lib/python2.7/site-packages/openpyxl/descriptors/base.py", line 44, in __set__
raise TypeError('expected ' + str(self.expected_type))
How do I use the preset color names?
Are you looking for something like this (which just worked for me):
sashighlight.fill = PatternFill(patternType='solid', fgColor=Color(colors.YELLOW))

Categories

Resources