I want to organize the (split)ed result vertically like
this
not this
import xlwings as xw
wb = xw.Book()
ws = wb.sheets.active
result = "1,2,3,4,5,6,7"
ws.range(1,1).value = result.split(",")
The xlwings tutorial says:
To write a list in column orientation to Excel, use transpose:
sht.range('A1').options(transpose=True).value = [1,2,3,4]
Related
Pandas.DataFrame, I have this output data as a dataframe and i wanted to write back this data back to excel.
This is excel sheet format
I wanted to write dataframe row in excel cell, for example :- Kosten EK will goes in excel sheet D4, IRR mit Finanzierung will go in excel sheet D5. I have same dataframe in which Soll-SOC 1-12 value is single value not an array and it is working properly, but for this case because of array i could not write. how can i solve this?
I am using xlwings, xlwriter to write data back to excel
import xlwings as xw
wb = xw.Book(file_path) # wb = xw.Book(filename) would open an existing file
Working_Sheet = wb.sheets["sheet_name"] # activating working sheet
Working_Sheet.range('D4:D15').options(index=False,header=False).value = Data[20000][0.25]
You should try to convert to a pd.DataFrame object.
import pandas as pd
import xlwings as xw
df = pd.DataFrame(...)
import xlwings as xw
wb = xw.Book(file_path) # wb = xw.Book(filename) would open an existing file
Working_Sheet = wb.sheets["sheet_name"] # activating working sheet
Working_Sheet.range('D4:D15').options(convert=pd.DataFrame, index=False,header=False).value = Data[20000][0.25]
What I want is with openpyxl to write a value I get form a len() or dups() to an excel cell.
Here are my imports:
import xlwings as xw
Here is the code:
#Load workbook
app = xw.App(visible = False)
wb = xw.Book(FilePath)
RawData_ws = wb.sheets['Raw Data']
Sheet1 = wb.sheets['Sheet 1']
RawData_ws['A1'] = (len(df.index))
Sheet1['B7'] = (len(df.index) - tot_dups))
RawData_ws['A2'] = (len(df.index)) #This one is after removing duplicate values
Tot_dups:
tot_dups = len(df.index)
I want the values of the different len() to show be written in the specific cells.
So, I already found the solution.
Change:
RawData_ws['A1'] = (len(df.index))
For:
RawData_ws['A1'].values = (len(df.index))
I can open a worksheet, how do I add the little filter menus to all columns without turning on any filters?
I can do it in xlsxwriter with
worksheet.autofilter(0, 0, 0, num_of_col)
How do I do it in openpyxl?
You can simply read ws.dimensions and it will return a string value with your range from "A1:XX". I used this to apply filters to my entire excel spreadsheet.
import openpyxl as px
wb= px.load_workbook('Data/Test01.xlsx')
ws = wb.active
ws.auto_filter.ref = ws.dimensions
wb.save('Data/Test03.xlsx')
All you need to do is to set worksheet.auto_filter.ref to the full range of worksheet cells.
import openpyxl
from openpyxl.utils import get_column_letter
workbook = openpyxl.load_workbook('Data/Test01.xlsx')
worksheet = workbook['Sheet1']
FullRange = "A1:" + get_column_letter(worksheet.max_column) \
+ str(worksheet.max_row)
worksheet.auto_filter.ref = FullRange
workbook.save('Data/Test03.xlsx')
I have an .xls Excel file with cells having background colors. I am reading that file into pandas with read_excel. Is there any way to get the background colors of cells?
The Solution suggested above works only for xls file, not for xlsx file. This raises a NotImplementedError: formatting_info=True not yet implemented. Xlrd library is still not updated to work for xlsx files. So you have to Save As and change the format every time which may not work for you.
Here is a solution for xlsx files using openpyxl library. A2 is the cell whose color code we need to find out.
import openpyxl
from openpyxl import load_workbook
excel_file = 'color_codes.xlsx'
wb = load_workbook(excel_file, data_only = True)
sh = wb['Sheet1']
color_in_hex = sh['A2'].fill.start_color.index # this gives you Hexadecimal value of the color
print ('HEX =',color_in_hex)
print('RGB =', tuple(int(color_in_hex[i:i+2], 16) for i in (0, 2, 4))) # Color in RGB
Brute-forced it through xlrd, as per Mark's suggestion:
from xlrd import open_workbook
wb = open_workbook('wb.xls', formatting_info=True)
sheet = wb.sheet_by_name("mysheet")
#create empy colormask matrix
bgcol=np.zeros([sheet.nrows,sheet.ncols])
#cycle through all cells to get colors
for row in range(sheet.nrows):
for column in range(sheet.ncols):
cell = sheet.cell(row, column)
fmt = wb.xf_list[cell.xf_index]
bgcol[row,column]=fmt.background.background_colour_index
#return pandas mask of colors
colormask=pd.DataFrame(bgcol)
Yet, there must be a better way thorugh pandas directly...
Improving on Sumit's answer (which should be the accepted one in my opinion), you can obtain the color for the whole column by using list comprehension:
import openpyxl
from openpyxl import load_workbook
excel_file = 'my_file.xlsx'
wb = load_workbook(excel_file, data_only = True)
sh = wb['my_sheet']
# extract color from column A.
color_in_hex = [cell.fill.start_color.index for cell in sh['A:A']]
I edited the code snippet from #csaladenes's response above based on this link, and it works for my xls file (the original resulted in all cells showing the same color index, though they have different background colors):
import xlrd
import numpy as np
wb = xlrd.open_workbook(file, formatting_info=True)
sheet = wb.sheet_by_name("mysheet")
bgcol=np.zeros([sheet.nrows,sheet.ncols])
for row in range(sheet.nrows):
for col in range(sheet.ncols):
c = sheet.cell(row, col)
cif = sheet.cell_xf_index(row, col)
iif = wb.xf_list[cif]
cbg = iif.background.pattern_colour_index
bgcol[row,col] = cbg
I have an Excel 2016 Book.xlsm. In the worksheet testsheet, the cells in the range A1:Y150 are filled with text or number contents. The upper-left cell is always A1.
I am using python v3 xlwings to open the Excel file.
import xlwings as xw
Book_name = 'C:/Users/name/Book.xlsm'
sheet_name = 'testsheet'
wb = xw.Book(Book_name)
sht = wb.sheets[sheet_name]
How do I find out the range of cells that are filled with contents using python, which in this case is A1:Y150?
You can get the range filled with contents with used_range:
import xlwings as xw
filename = "test.xlsx"
wb = xw.Book(filename)
ws = wb.sheets["SheetX"]
a_range = ws.used_range.address
print(a_range)
If wb is defined as Excel Workbook, then this is a good way:
print (wb.sheets[sheet_name].api.UsedRange.Address)