being lazy I kept your piece of code that converts from base10 to base7 and added something to convert to base10.
def fromtenthtoseven(x, frombase, tobase):
result=" "
if (frombase != 10):
sx = str(x)
p = 1
b = 0
while (sx != ""):
d = int(sx[-1])
sx = sx[:-1]
b = b + d * p
p = p * frombase
else:
b = x
while(b>0):
result+=str(b%tobase)
b=b//tobase
return(int(result[::-1]))
print(fromtenthtoseven(512, 10, 7))
print(fromtenthtoseven(1331, 7, 10))
print(fromtenthtoseven(1331, 7, 5))
The result looks like
$ python x.py
1331
512
4022
There are certainly a billion ways to do this better, but it works