I am creating this excel report for vat summary for every invoice lines
In an invoice there can be more than one invoice line/journal item
for the same I used the below code
if len(invoice.invoice_line_ids) > 1:
worksheet.write(row, 3, invoice.invoice_line_ids[0].product_id.name)
for product_id in invoice.invoice_line_ids[1:]:
row += 1
worksheet.write(row, 3, product_id.name)
else:
worksheet.write(row, 3, invoice.invoice_line_ids[0].product_id.name)
for a product name its coming correctly
but when I copy the same code for other fields such as VAT name, price subtotal etc its showing as in the image
report sample image
Please check out my full code
#code for Product name which is correct
if len(invoice.invoice_line_ids) > 1:
worksheet.write(row, 3, invoice.invoice_line_ids[0].product_id.name)
for product_id in invoice.invoice_line_ids[1:]:
row += 1
worksheet.write(row, 3, product_id.name)
else:
worksheet.write(row, 3, invoice.invoice_line_ids[0].product_id.name)
#code for vat name which is not correct
if len(invoice.invoice_line_ids) > 1:
worksheet.write(row, 4, invoice.invoice_line_ids[0].tax_ids.name)
for tax_ids in invoice.invoice_line_ids[1:]:
row += 1
worksheet.write(row, 4, tax_ids.name)
else:
worksheet.write(row, 4, invoice.invoice_line_ids.tax_ids.name)
#code for subtotal which is not correct
if len(invoice.invoice_line_ids) > 1:
worksheet.write(row, 5, str(invoice.invoice_line_ids[0].price_subtotal))
for price_subtotal in invoice.invoice_line_ids[1:]:
row += 1
worksheet.write(row, 5, str(price_subtotal))
else:
worksheet.write(row, 5, str(invoice.invoice_line_ids.price_subtotal))
Related
I'm trying to display a basic SQLite database as a table in pygame. It holds the players username and score. The code I have now displays it but it doesn't aligne nicely. The code:
def leader_board():
i = 35
messg = font_style.render(f'PLAYER SCORE', True, yellow)
dis.blit(messg, [dis_width / 5, (700 / 4) + 5])
cur.execute('SELECT * FROM snake_score ORDER BY score desc LIMIT 10')
rows = cur.fetchall()
for row in rows:
mesgg = font_style.render('{:>3} {:30}'.format(row[0], row[1]), True, yellow)
dis.blit(mesgg, [dis_width / 5, (700 / 4) + i + 5])
i += 35
This is the result that I get from it:
I'd like the numbers to be all aligned with the end of the word 'score'. Any help would be appreciated because I have tried some things and none seemed to work.
Either use a monospace font or render the text of each column separately:
def leader_board():
i = 35
column_space = 400
head1 = font_style.render(f'PLAYER', True, yellow)
head2 = font_style.render(f'SCORE', True, yellow)
dis.blit(head1, [dis_width / 5, (700 / 4) + 5])
dis.blit(head2, [dis_width / 5 + column_space, (700 / 4) + 5])
cur.execute('SELECT * FROM snake_score ORDER BY score desc LIMIT 10')
rows = cur.fetchall()
for row in rows:
column1 = font_style.render('{:>3}'.format(row[0]), True, yellow)
column2 = font_style.render('{:30}'.format(row[1]), True, yellow)
dis.blit(column1, [dis_width / 5, (700 / 4) + i + 5])
dis.blit(column2, [dis_width / 5 + column_space, (700 / 4) + i + 5])
i += 35
I need to add form field with to every cell in a table. Here is what I have so far.
Set csvTable = ActiveDocument.Tables(2)
With csvTable
For r = 1 To UBound(csvData)
.cell(r + 4, 1).Range.FormFields.Add(Range:=.cell(r + 4, 1).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(0)
.cell(r + 4, 3).Range.FormFields.Add(Range:=.cell(r + 4, 3).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(3)
.cell(r + 4, 5).Range.FormFields.Add(Range:=.cell(r + 4, 5).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(4)
.cell(r + 4, 4).Range.FormFields.Add(Range:=.cell(r + 4, 5).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(5)
.cell(r + 4, 5).Range.FormFields.Add(Range:=.cell(r + 4, 5).Range, Type:=wdFieldFormTextInput).Result = csvData(r)(6)
Next
End With
The code seems to add form fields as wanted to all the cells, but the text added ends up only in the first column (i.e. for each row, the text is iteratively updated in the first column only and I end up with csvData(r)(6) in the first column of each row). Any help would be appreciated!
I have a dataframe with two integer columns that represent the start and end of a string of text. I'd like to group my rows by length of text (end - start), but with a margin of error of +- 5 characters so that something like this would happen:
start end
0 251
1 250
2 250
0 500
1 500
0 499
How would I achieve something like this?
Here is the code I am using right now
d = {'text': ["aaa", "bbb", "ccc", "ddd", "eee", "fff"],
'start': [0, 1, 0, 2, 1, 0],
'end': [250, 500, 501, 251, 249, 499]}
df = pd.DataFrame(data=d)
df = df.groupby(['start', 'end'])
I ended up solving the problem by rounding the length of my text.
df['rounded_length'] = (df['end'] - df['start']).round(-1)
df = df.groupby('rounded_length')
All my values become multiples of 10, and I can group them this way.
List
L= [23, 91, 0, -11, 4, 23, 49]
Code
for i in L:
if i > 10:
num = i * 30
else:
num = i * 1
if num % 2 == 0:
num += 6
if i > 50:
num -= 10
if i != -11:
num += 10
print(num)
Output
696
2736
6
-11
10
696
1476
I'm trying to sum the numbers in the output and then divide the total by 2.
Initialize a variable sum outside the loop then, add the value of num to sum before the print statement. Finally, print(sum/2) once outside the loop.
sum = 0
for i in L:
...
sum += num
print(num)
print(sum/2)
I have a nested for loop that is looking to take values in one row, generate a value based on an equation, then do the same for many rows following the first one, all while adding the value together.
Essentially, if row one has a value of 15, and row 2 and 3 return values of 10 and 12, the variable storing the total value (named genCost) will be 37.
I want to place the summed total values of genCost in a new sheet, separated by day, but when I run the code I get a Run-time 1004 error. I realize that this has something to do with the sheet that I am working on, and the sheet that I am trying to place the values into (the 2nd to last line in the code).
I understand my code may be ugly and simple, but can somebody help me troubleshoot this?
'Nested For loop for ALL OTHER DAYS of genCost...only 1 formula
For j = 2 To dayNumber
For i = 1 To increments
'IF(AND(U7=1,U6=0),R7,0)
If Cells(rowValue, 21) = 1 And Cells(rowValue - 1, 21) = 0 Then
ifValue = Cells(rowValue, 18)
Else
ifValue = 0
End If
'calculate value variable with second half of equation
value = (((Cells(rowValue, 23) * Cells(rowValue, 16) * (1 / 6)) + (Cells(rowValue, 25) * Cells(rowValue, 17) * (1 / 6)) + (Cells(rowValue, 21) * Cells(rowValue, 19) * (1 / 6)) + ifValue))
genCost = genCost + value
'set value and ifValue back to zero and step down one row and do again
value = 0
ifValue = 0
rowValue = rowValue + 1
Next i
Cells(3, j) = genCost
Dim genCostRefNum As Integer: genCostRefNum = 6
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
genCost = 0
Next j
Instead of this
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4)) = genCost
write this
ThisWorkbook.Sheets(1).Cells(genCostRefNum, 4) = genCost
There is a default property of Cells statement, which is Value (same for Range). So your code was exactly
ThisWorkbook.Sheets(1).Range(Cells(genCostRefNum, 4).Value).Value = genCost
and after executing Cells something like:
ThisWorkbook.Sheets(1).Range(6).Value = genCost
It's good practice to always write complete statements and don't rely on default properties.