I've been trying to use Openpyxl to split the Excel screen vertically (in Excel the Split button in the View tab on the ribbon). I haven't found any guide, how to do this. But I have found this web page (https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.views.html) and I think the "ySplit" property may be the solution. Unfortunately I haven't been able to figure out how to use it properly. I've tried the following code:
wb = openpyxl.load_workbook('file.xlsx')
sh = wb.active
sh.sheet_view.pane.ySplit = 20
EDIT: But this code does not work: AttributeError: 'NoneType' object has no
attribute 'ySplit'.
I've also tried some variations of the code above (with ySplit). But without success. If anybody could help me to find a way, how to split the screen, it would be much appreciated.
Thanks in advance.
EDIT2: The solution was provided by stovfl in comments. The code
should be:
sh.sheet_view.pane = openpyxl.worksheet.views.Pane(xSplit=20.0, ySplit=None, topLeftCell='C1', activePane='topLeft', state='split')
Question How to split Excel screen with Openpyxl?
To define, to show a Worksheet splitted, you have to create a openpyxl.worksheet.views.Pane object and assign it to myWorksheet.sheet_view.pane.
from openpyxl.worksheet.views import Pane
wb = openpyxl.load_workbook('file.xlsx')
ws = wb.active
ws.sheet_view.pane = Pane(xSplit=20.0, ySplit=None,
topLeftCell='C1', activePane='topLeft', state='split')
wb.save('file.xlsx')
openPyXL - worksheet.views.Pane
class openpyxl.worksheet.views.Pane(xSplit=None, ySplit=None,
topLeftCell=None,
activePane='topLeft', state='split')[source]
activePane
Value must be one of {‘topLeft’, ‘bottomRight’, ‘topRight’, ‘bottomLeft’}
state
Value must be one of {‘split’, ‘frozen’, ‘frozenSplit’}
topLeftCell
Values must be of type <class ‘str’>
xSplit
Values must be of type <class ‘float’>
ySplit
Values must be of type <class ‘float’>
Related
My organization has a clean export for bills of materials (BOM). I would like to automatically parse the excel file to check the BOM for certain attributes.
At the moment, I'm using Python with openpyxl.
I can read the excel workbook and worksheet just fine, but I cannot seem to find the attribute that contains the "outline level" of each row (I fully concede that I may be using the wrong terminology... another term candidate might be "group").
When I look at my excel file using excel, I see this at the left of the screen:
I would like to extract the 1 2 3 4 5 from each of the rows and to tell what grouping they were in.
My initial code is:
from pathlib import Path
import openpyxl as xl
path = Path('<path-to-my-file>.xlsx')
wb = xl.load_workbook(filename=path)
sh = wb.worksheets[0]
# ... would like to put outline level reading code here
From reading other questions, I suspect that I need to look at the row_dimension.group method of the worksheet, but I can't seem to get a handle on the syntax or the exact attribute that I'm looking for.
Thanks for the post. I was struggling with the same problem and seing your post gave me an idea!
I overcome it with the following code:
from pathlib import Path
import openpyxl as xl
path = Path('<path-to-my-file>.xlsx')
wb = xl.load_workbook(filename=path)
sh = wb.worksheets[0]
for row in sorted(sheet.row_dimensions):
outline1=sheet.dimensions[row].outlineLevel
outline2=sheet.dimensions[row].outline_level
print(row,sheet.dimensions[row], outline1, outline2 )
Maybe you can use the following code to gather individual row outline levels as an integer. I use a similar code to find maximum outline level in a sheet with some more lines.
for index in range(ws.min_row, ws.max_row):
row_level = ws.row_dimensions[index].outline_level + 1
In here row level variable is the outline level, you may use as required. But please double check +1, if I remember correctly, to get true level, you need to increase variable by one.
I have been writing a script using the openpyxl module for python 3.8.3. In my script I am taking a pre-existing excel workbook with multiple sheets. This sheet is coming from another person, and they want me to only run the script on the tabs that are colored with a specific color. I have been attempting to recognize what worksheets have the specified color by using the tabColor in sheet_properties, but that does not work for me. Any help is appreciated!
Here
def excel_modifications(file_path, storage_path):
initial_wb = load_workbook(file_path, data_only=True, read_only=True)
for ws in initial_wb:
if ws.sheet_properties.tabColor == "11217371":
common_compression(ws, storage_path)
To anyone who is encountering a similar problem to me I found my solution. The parameters within which is used by sheet_properties.tabColor contain both a 'rgb' parameter and a 'theme' parameter. In my case, the workbook tabs from the work book I worked on were colored by theme colors, not a rgb value chosen by the person writing the excel sheet. On my workbook, the writer colored their tab with the Accent 6 in the default theme colors. So for me, the solution was to look for the index of the given sheet_properties.tabColor.theme that is equal to 6, as by starting from zero, the color I was searching for was the 9th index on the list provided by excel.
Here is my working conditional fixing my problem above:
def excel_modifications(file_path, storage_path):
initial_wb = load_workbook(file_path, data_only=True, read_only=True)
for ws in initial_wb:
if ws.sheet_properties.tabColor.theme == 6:
common_compression(ws, storage_path)
Here is a screenshot from the listing discussed of Accents in excel:
Example Accent listing from excel
I am very new to Python world. Pardon for any inane mistake(s).
What program is trying to do?
I just wrote a piece of code which reads the data from an existing excel and printing a selected cell value.
Problem?
Problem is IDE is not showing all the suggestion like when control loaded the sheet (of type Worksheet) the object name "sheet" does not show methods available in sheet object.
Code
import pandas as pd
from openpyxl import load_workbook
test_f_path = '/Users/new_python_user/_Codes/_Personal/test_resources/Test_Update.xlsx'
sheet_name = 'Eight'
workbook = load_workbook(test_f_path)
sheet = workbook[sheet_name]
print(sheet.cell(1, 1).value) # <--- Here is problem: typing "sheet." does not provide suggestions
Pycharm Version
Pycharm Community 2019.3.3 for MAC
EDIT-1
Modified the program to cast the object then it worked. But this is not the way i thought about Python. May be i am missing something. Pls advice.
from openpyxl import load_workbook
from openpyxl.worksheet.worksheet import Worksheet # <--Imported here
test_f_path = '/Users/i852841/_Codes/_Personal/PyStockCrawler/test_resources/Test_Update.xlsx'
sheet_name = 'Eight'
workbook = load_workbook(test_f_path)
sheet = workbook[sheet_name]
sheet_casted = Worksheet(sheet) #<-- Cast here
I'm using win32com to fill out an Excel spreadsheet with some analytic information. I have a cell that I want to be in this form:
This is the problem description: this is the command you run to fix it
I can't seem to figure out how to get the text into the cell with Python with mixed formatting.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
excel.Visible=True
sel = excel.Selection
sel.Value = "this is the command you run to fix it"
sel.Font.Bold = True
ws.Cells(1,1).Value = 'This is the problem description' + sel.Value #makes the whole cell bold
ws.Cells(1,1).Value = "{} {}".format("this is the problem desc",sel) #makes the whole cell bold
The selection object has a Characters attribute, but I can't find any documentation on what it does.
Per #ron-rosenfield, the VBA code to pull this off looks like:
Range("A1").Characters(2,5).Font.Bold = true
And similarly, there is a Characters object attribute of the excel WorkSheet's Range
>>> ws.Range("A1").Characters
<win32com.gen_py.Microsoft Excel 14.0 Object Library.Characters 967192>
HOWEVER, the method you need to access a range with that object is GetCharacters(start, length) (index starts at 1 as per usual with the excel com methods)
ws.Range("A1").GetCharacters(2,4).Font.Bold = True
You can use this command to update the boldness of characters in a cell after the fact.
Although you are looking for a win32com solution in the body of your question I'll point out for anyone who gets here via the title that this is also possible in Python with XlsxWriter.
Example:
I had intended to do this using the code in the answer here, in the last block of code. However i get an error in the line for cell in ws.iter_rows(range_string=range_expr): saying that "Worksheet object has no attribute iter_rows". Any idea what I'm doing wrong here?
all i needed was to change the workbook declaration to the following: wb = load_workbook('path/doc.xls', use_iterators=True), adding in the use_iterators paramater. Simple issue, simple solution :)