I am using xlrd, xlwt, and xlutils on the Windows Vista OS with Python 2.7. I have a set of DNA sequences in an excel worksheet that are 100 characters long, with each sequence in a single cell. I am trying to highlight characters at specific positions within each of these sequences in excel (bold them or change color), but have not found a way to format individual characters within a cell. Applying a style applies it to the entire cell to my knowledge. Therefore I am trying to break the sequences down into individual components where some parts of the sequence will be modified with a style while others won't, and to then collate these back together into a single string in a single cell.
Code:
rb = open_workbook('Mybook', formatting_info=True)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)
minus35style = xlwt.easyxf('font: bold 1') # style I'd like for just one character
for b in range(0, 368, 1):
rscellin = rs.cell(b,9)
f = rscellin.value
tominus35 = str(f[0:34])
minus35 = str(f[35:36])
ws.write(b, 14, tominus35)
ws.write(b, 14, minus35, minus35style)
wb.save('Mybook')
My problem is that adding a style changes the whole cell, and I want just certain characters to be modified. Additionally, subsequent writing to the same cell overwrites what was there previously. Is there a way to modify individual characters in a single cell, or to add differently formatted text to an existing cell that already has text in it?
Please let me know if you require additional information that I've overlooked. I appreciate your time in advance.
Brett
Recent versions of xlwt include the ability to use Rich Text within a cell. Where normally you would use ws.write, use ws.write_rich_text instead. The first two parameters are the row index and column index, as usual; but the next parameter is a sequence of components. Each component can either be a "naked" text value or a (text, font) pair. The naked text values will use the font from the cell's overall style, which can be specified using the optional fourth parameter.
For the (text, font) pairs, it is simplest to generate fonts using the new easyfont feature, which is kind of like easyxf but only for fonts. Here is an example:
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')
font0 = xlwt.easyfont('')
font1 = xlwt.easyfont('bold true')
font2 = xlwt.easyfont('color_index red')
style = xlwt.easyxf('font: color_index blue')
seg1 = ('bold', font1)
seg2 = ('red', font2)
seg3 = ('plain', font0)
seg4 = ('boldagain', font1)
ws.write_rich_text(2, 5, (seg1, seg2, seg3, seg4))
ws.write_rich_text(4, 1, ('xyz', seg2, seg3, '123'), style)
wb.save('rich_text.xls')
You should be able to adapt the above for your purposes. Note that you still have to write or overwrite the whole cell at once; you can't go back and update only part of a cell later.
Related
I've looked over the Openpyxl docs, styles, cell module, and source code, but I'm not seeing what I need. Maybe I missed it (as new to Python).
I need to format certain cells within a spreadsheet(ss) differently from the rest of the ss. Am I able to specify an individual cell as shrink-to-fit, align right, align bottom, font color = grey, etc., while keeping the rest of the SS in the original style? Is this a Cell style that should be set or is there another resource to see what attributes Openpyxl will allow on an individual cells?
Here's a code snippet where the 'al' variable is working, but not the 'br', and I don't know why.
# Cell Alignment
al = Alignment(horizontal='center', vertical='center')
br = Alignment(horizontal='right', vertical='bottom')
for row in sheet['A1':'I43']:
for cell in row:
if cell == 'Hz':
cell.alignment = br #Help: not working
else:
cell.alignment = al
Thanks,
Phil
Ok, I think I've got it. So far, I'm accomplishing individual cell formatting using the NamedStyle object.
Here is my code:
# Set 'Named Styles', which are mutable, when need to apply formatting to different cells at once
headerrows = NamedStyle(name='headerrows')
headerrows.font = Font(bold=True, underline='none', sz=12)
headerrows.alignment = al
rooms = NamedStyle(name="rooms")
rooms.font = Font(bold=True, size=12)
rooms.border = Border(left=bold, top=bold, right=bold, bottom=bold)
sheet['A1'].style = rooms
sheet['A2'].style = headerrows
sheet['A3'].style = 'headerrows'
sheet['A4'].style = 'rooms'
I want to split the text of a textbox after a specific amount of visible lines.
I've found some codes that "allows that", but all of them consider the lines by the "vbCrLf" parameter, but i want to split using the visible lines of a multiline textbox.
To make it more clear to understand, consider a multiline textbox with the following text:
"The history of textbooks dates back to civilizations of ancient history. For example, Ancient Greeks wrote texts intended for education. The modern textbook has its roots in the standardization made possible by the printing press. Johannes Gutenberg himself may have printed editions of Ars Minor, a schoolbook on Latin grammar by Aelius Donatus. Early textbooks were used by tutors and teachers, who used the books as instructional aids (e.g., alphabet books), as well as individuals who taught themselves."
When i use the Textbox.Linecount function it returns the number 6 because the textbox shows six lines (which depends on the size of the control), but if i use a function like strText = Split(TextBox.Text, vbCrLf) it will return 1, because there is only one vbCrLf. But i need to split the text into two textbox considering the visible lines of the control, something like what happens in page breaks of MS Word.
For a better visual explanation, please look at the attached image.
Example
Firstly, I'm not convinced there is a robust and elegant way to do this, but it was fun to experiment and it might be useful to you.
The following will split the contents of TextBoxInput into TextBoxPage1 and TextBoxPage2 breaking on the line number specified by PAGED_TEXT_BOX_LINES.
It uses the textbox itself to detect natural line breaks and thus implicitly caters for the size of the textbox, the font, etc.
The desired line count is hard coded as a constant - not doing this would require an alternative of calculating the line height of the textbox (requiring calculations based on the font metrics and the textbox's internal line-leading size).
It only handles two "pages". But the concept could be extended simply by repeating the process based on the remainder of text that ends up in TextBoxPage2.
Private Sub CommandButton1_Click()
Const PAGED_TEXT_BOX_LINES As Integer = 5
Dim text As String
Dim i As Long
Dim textLength As Long
Dim curLine As Integer
text = TextBoxInput.text
textLength = Len(text)
TextBoxPage1.SetFocus
'add characters of the input string until the first page textbox
' exceeds maximum line count
For i = 1 To textLength
TextBoxPage1.text = Mid$(text, 1, i)
If TextBoxPage1.LineCount > PAGED_TEXT_BOX_LINES Then
'retreat cursor until we reach previous line, so we can
' detect the word that wrapped
curLine = TextBoxPage1.curLine
Do While TextBoxPage1.curLine = curLine
TextBoxPage1.SelStart = TextBoxPage1.SelStart - 1
Loop
'the remaining text after the SelStart is what
' wrapped, so stop page 1 after SelStart
TextBoxPage1.text = Mid$(text, 1, TextBoxPage1.SelStart)
TextBoxPage2.text = Trim$(Mid$(text, TextBoxPage1.SelStart + 1))
Exit For
End If
Next i
End Sub
I am implementing a search function in python with Tkinter and would like to select the first match it comes to. I have seen many examples with creating a tag_config to highlight the background of the indexed range, however I would like to select the text (the same way one would by clicking at the first index, then shift clicking the last index).
Thus far I have got both the start and end index of the area I need to select, I just don't know the command to "select" the text with that information.
My current code (that uses a highlight approach) is:
def search_command():
word = askstring("Search", "Enter word to search")
length = len(str(word))
pos = textPad.search(word, '1.0', stopindex=END)
row, col = pos.split('.')
endlen = int(col) + length
end = row + '.' + str(endlen)
textPad.tag_add("found", pos, end)
The "found" tag just highlights the background of the text rather than selecting it.
Any help with finding the correct function would be greatly appreciated.
The selection is defined by the "sel" tag. Apply that tag to the range of text you want selected:
textPad.tag_add("sel", pos, end)
I am trying to read a cell with possible trailing zeros as a string instead of numeric (which strips off leading zeros). The cell is read by integer column/row as below instead of column string as this answer has.
initial code
$instReader = $reader->load($this->file);
$sheet = $instReader->getSheet(0);
I tried modifying this from:
$keyCell = $sheet->getCellByColumnAndRow(1,5);
to:
$sheet->setCellValueExplicitByColumnAndRow(1,5, PHPExcel_Cell_DataType::TYPE_STRING);
$keyCell = $sheet->getCellByColumnAndRow(1,5);
the former gives 1407 for $keyCell instead of 01407
the latter gives "s" or ""
how do I treat the cell as string before calling getCellByColumnAndRow and using only integer values for column and row.
(BTW, if this can be done once for an entire column instead of each time for each individual cell that would be better)
$keyCell = $sheet->getCellByColumnAndRow(1,5)->getValue();
Will read the cell data in the format that it's actually stored by Excel, you can't arbitrarily change that or tell PHPExcel to read it as a different datatype.
However, if the cell has formatting applied, then you can use
$keyCell = $sheet->getCellByColumnAndRow(1,5)->getFormattedValue();
instead, and this will return the data as a string, with whatever format mask was defined in the Excel spreadsheet
Same issue for me. I become crazy.
Tried to set
$objReader->setReadDataOnly(true);
wasn't working
tried
$sheet->getCellByColumnAndRow(4,$row)->getValue()
because normaly display text as raw => doesn't working.
So last I change code in library. Edit file named DefaultValueBinder.php
Search for dataTypeForValue function and set this :
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_STRING;//TYPE_NUMERIC patch here;
} elseif (preg_match('/^\-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)$/', $pValue)) {
return PHPExcel_Cell_DataType::TYPE_STRING;//TYPE_NUMERIC patch here;
So now return numbers with 0
I have googled a while for the methods to create excel files (*.xlsx, not the csv files) using programming languages, such as C++ or PHP. here is an example, http://www.the-art-of-web.com/php/dataexport/
But Ideally, I want to be able to specify the colours for each cell. For example, in VBScript using COM object Excel.Application, the code looks like:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)
With objWorksheet
.Cells(1,1).Interior.Color=RGB(245,245,245)
...
...
...
End With
objWorkbook.SaveAs("sample.xlsx")
objExcel.Quit
How can I do this without the use of COM object? I need the program to work platform-independent so COM object is not a good choice.
Update: here is an interesting post, that generates the VBS file and double click the VBS file will give you a nice picture in Excel by drawing cells with different colors.
You can modify cell backgrounds using python and the XLSX Writer module. While it is not the C++ or PHP language you mentioned, Python is cross platform and the code below works on the Fedora box I tested with.
First, you need python and the module mentioned above. You can install the module by doing this:
pip install xlsxwriter
Next, we are going to modify the first tutorial to provide a little color to the "Total" value.
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0
green_format = workbook.add_format()
green_format.set_pattern(1) # This is optional when using a solid fill.
green_format.set_bg_color('#008000')
# Iterate over the data and write it out row by row.
for item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)', green_format)
workbook.close()
The things that were added are these lines. This utilizes the set_bg_color function.
green_format = workbook.add_format()
green_format.set_pattern(1) # This is optional when using a solid fill.
green_format.set_bg_color('green')
And the =SUM row was modified to use this green format:
worksheet.write(row, 1, '=SUM(B1:B4)', green_format)
This code creates a green cell at B5.
A note about the color: I provided the color 'green', because that is one of the few colors that are mapped to an RGB value. If you need to use something other than these colors, you can provide an RGB value. #008000 maps to green
green_format.set_bg_color('#008000')
This line is functionally identical to the one provided above.
If you want to use PHP, there are at least two different toolkits, which will generate Excel files without the use of COM Object :
PhpExcel and ExcelWriterXML.
For ExcelWriterXML for instance, changing the background color is $format->bgColor('Black'); , see complete example on the site.
Another way is to produce xlsx files (which are XML inside) through a template engine like TinyButStrong.